tirenweb
tirenweb

Reputation: 31749

symfony2: trying to pass parameters on the URL

I have this routing.yml inside a bundle:

project_backend_update_item:
    path:     /update-item/{id}
    defaults: { _controller: ProjectBackendBundle:Default:updateItem }

and this inside my controller:

public function updateItemAction(Request $request)
{

    $repository = $this->getDoctrine()
        ->getRepository('ProjectFrontendBundle:Item');
    var_dump($request->query->get('id'));

And when I request: "app_dev.php/update-item/1" I get NULL. Why? I expected "1".

Upvotes: 0

Views: 95

Answers (2)

Yann Eugoné
Yann Eugoné

Reputation: 1351

In Symfony, whenever you have a parameter in your route pattern, you may add a variable with the same name in your controller action.

In your case, you should do something like this :

public function updateItemAction($id, Request $request)
{
    $repository = $this->getDoctrine()
        ->getRepository('ProjectFrontendBundle:Item');

    var_dump($id);
}

Upvotes: 0

griotteau
griotteau

Reputation: 1822

$request->query give you the $_GET parameters (for example with /update-item?id=5) Your parameter 'id' is not passed with _GET, but with routing.

You must do :

public function updateItemAction($id)
{
    var_dump($id);
}

Or

public function updateItemAction(Request $request, $id)
{
    var_dump($id);
}

Upvotes: 2

Related Questions