Adri HM
Adri HM

Reputation: 3060

Send parameter in redirectToRoute and Get It in the controller

I attempt to send a parameter in the redirection but the get in the controller return null :

Redirection :

    public function taskInUser(User $user): Response
    {
        return $this->redirectToRoute('task_add', ['user' => $user]);
    }

In the Controller:

    /**
     * @Route("/add", name="task_add")
     *
     */
    public function addAction(User $user = null): Response
    {
        dump($user);die();
        ...
    }

I attempt also that:

    /**
     * @Route("/add", name="task_add")
     *
     */
    public function addAction(Request $request): Response
    {
        dump($request->query->get('equipment');die();
        ...
    }

And It didn't work. I receive null in the controller but I checked and it send a good value.

Upvotes: 0

Views: 574

Answers (1)

threeside
threeside

Reputation: 358

Why you don't add the parameter on your route declaration ? Look here :

https://symfony.com/doc/current/routing.html#route-parameters

You can use User id as int :

    /**
     * @Route("/add/{user_id}", name="add_user")
     */
     public function addAction(int $user_id)

or use ParamConverter to convert id to User without use Repository function.

https://symfony.com/doc/current/routing.html#parameter-conversion

Upvotes: 1

Related Questions