How to pass variables to a RedirectResponse

Viewed 37

I've a variable name $email but I don't know how to send it to dashController.php:

$action = $this->client->auth($username, $password);

    if (isset($action->success) && $action->success){

        $email = $action->data->email;

        $response = new RedirectResponse('/dash');
        $cookie = new Cookie('JWT', $action->data->jwt->token, new \DateTime($action->data->jwt->expires_at));
        $response->headers->setCookie($cookie);

        return $response;
    }

in dashController.php

class dashController extends authContainer {

#[Route("/dash", "dashboard")]
public function indexAction(): Response
{

        return $this->render('dash.twig', [
            'email' => ...
        ]);

}}
1 Answers

If your class inherit from Symfony AbstractController class, you can use the redirectToRoute method helper. Your code will be like :

if (isset($action->success) && $action->success){

    $email = $action->data->email;

    $response = $this->redirectToRoute('dashboard');
    $cookie = new Cookie('JWT', $action->data->jwt->token, new \DateTime($action->data->jwt->expires_at));
    $response->headers->setCookie($cookie);

    return $response;
}
Related