Symfony2 - Routing with query in routing.yml
I wish to enable variables to be added to the URL for a route in Symfony2 ie.
www.mysymfonyproject.com/blog/1?style=fresh&rpp=5
The documentation talks about generating urls with query, but from what I understand this is for dynamic content in the app?:
$router->generate('blog', array('page' => 2, 'category' => 'Symfony'));
// /blog/2?category=开发者_Python百科Symfony
What I would like would be this:
_blogList:
pattern: /blog/{page}?{query}
defaults: { _controller: TestBundle:Blog:view ,page:1, query: NULL }
But obviously that doesn't work.
Can this be done using YAML? Or do I need to switch my config.yml to PHP?
Thanks.
You can access all the query parameters (the parameters that isn't part of the actual route pattern) by doing:
$this->getRequest()->query->get('parameter_name')
inside the action of your controller that matches the route.
I have some vague memory of there being some shortcut for this (like $this->getParameter() or $this->getQuery()), but I'm not sure since I couldn't find it documented anywhere.
Can you try this route (it should work) :
blog:
pattern: /blog/{page}
defaults: { _controller: TestBundle:Blog:view ,page:1, style:fresh, rpp:5 }
Then to generate an url like
/blog/1?style=fresh&rpp=5
, use:
$router->generate('blog', array('page' => 1, 'style' => 'fresh', 'rpp' => 5));
or
$router->generate('blog', array('page' => 1));
精彩评论