Sruj
Sruj

Reputation: 1207

Why this not passing variable from view to controller?

Befeore you click minus just write a short answear. Not so many twig advice in the web. I'm beginner.

View

#\src\BookBundle\Resources\views\Default\index.html.twig
<a href="{{ path('add',{'name':'John','surname':'Smith'})}}">Add John to DB</a>

Controller

#\src\BookBundle\Controller\DefaultController.php
 /**
 * @Route("/add.html", name="add")
 * @Template()
 */

public function addAction($name,$surname)
{
    //some db code
    return new Response('New name '.$name);
}

Upvotes: 0

Views: 1113

Answers (1)

Federkun
Federkun

Reputation: 36924

You have not definited any slug placeholder for $name and $surname in your route, so path('add',{'name':'John','surname':'Smith'}) will append the data to the query string. Something like /add.html?name=John&surname=Smith. So, you can get those parameters in your controller with $request->query->get('name');. See http://symfony.com/doc/current/book/controller.html#the-request-as-a-controller-argument

Remember that when you use the @Template annotation, the controller should return an array of parameters to pass to the view.

Upvotes: 2

Related Questions