BobFlemming
BobFlemming

Reputation: 2040

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=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.

Upvotes: 1

Views: 8838

Answers (2)

Manne W
Manne W

Reputation: 1459

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.

Upvotes: 3

julesbou
julesbou

Reputation: 5780

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));

Upvotes: 1

Related Questions