Symfony2: How to pass url querystring parameters to controllers?
Solution 1
To work with GET / POST parameters in a controller that extends Symfony\Bundle\FrameworkBundle\Controller\Controller:
public function updateAction()
{
$request = $this->getRequest();
$request->query->get('myParam'); // get a $_GET parameter
$request->request->get('myParam'); // get a $_POST parameter
...
}
For a controller which does not extend the Symfony base controller, declare the request object as a parameter of the action method and proceed as above:
public function updateAction(Request $request)
{
$request->query->get('myParam'); // get a $_GET parameter
$request->request->get('myParam'); // get a $_POST parameter
...
}
Solution 2
You can't specify your query string parameters in the routing configuration files.
You just get them from the $request object in your controller: $request->query->get('foo');
(will be null
if it doesn't exist).
And to generate a route with a given parameter, you can do it in you twig templates like that :
{{ path(route, query|merge({'page': 1})) }}
If you want to generate a route in your controller, it's just like in the documentation you linked:
$router->generate('blog', array('page' => 2, 'category' => 'Symfony'));
will generate the route /blog/2?category=Symfony
(the parameters that don't exist in the route definition will be passed as query strings).
jcroll
Updated on May 21, 2020Comments
-
jcroll almost 4 years
Maybe I am missing something but there doesn't seem to be a way to define querystring parameters in routes in Symfony2 so that they can be passed into a controller.
For instance, instead of generating a URI like
/blog/my-blog-post
(from Symfony2's routing documentation) and passing it to the following route:# app/config/routing.yml blog_show: pattern: /blog/{slug} defaults: { _controller: AcmeBlogBundle:Blog:show }
I would prefer to generate a URI like
/blog?slug=my-blog-post
. The problem is I can't find anywhere to define theslug
parameter in the route configuration file (like its {slug} counterpart above).Perhaps this is on purpose but then what is the best practice for working with GET parameters in the querystring?
The documentation does make mention of them in Generating URLs with Query Strings, so how to pass them into the controller?
Where I can find mention of them is Symfony2 and HTTP Fundamentals:
use Symfony\Component\HttpFoundation\Request; $request = Request::createFromGlobals(); // retrieve GET variables $request->query->get('foo');
Is this the best practice for working with them inside the controller?
-
Brewal about 10 yearsSince Symfony 2.4, the
getRequest()
method is now deprecated and one should use the second trick, with theRequest
parameter in the controller method. thegetRequest()
method will be removed in Symfony 3. -
fritzmg almost 10 years@Brewal: but if you have a
Controller
that extendsSymfony\Bundle\FrameworkBundle\Controller\Controller
and which already gets parameters as defined in its route, can you still also get the Request this way or do you simply have to useRequest::createFromGlobals()
now? -
Brewal almost 10 yearsYou can still get the request thanks to the
Request
type specified just before the$request
variable. Symfony is smart enough to guess that. -
Stphane over 7 yearsDocumentation: generating-urls-with-query-strings