4
votes

In Symfony2, the route parameters can be automatically map to the controller arguments, eg: http://a.com/test/foo will return "foo"

    /**
     * @Route("/test/{name}")
     */
    public function action(Request $request, $name) {
        return new Response(print_r($name, true));
    }

see http://symfony.com/doc/current/book/routing.html#route-parameters-and-controller-arguments

But I want to use query string instead eg: http://a.com/test?name=foo

How to do that ? For me there are only 3 solutions:

Is there another solution ?

3

3 Answers

8
votes

I provide you the code for those which want to use a converter :

use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface;
use Symfony\Component\HttpFoundation\Request;

/**
 * Put specific attribute parameter to query parameters
 */
class QueryStringConverter implements ParamConverterInterface{
    public function supports(ParamConverter $configuration) {
        return 'querystring' == $configuration->getConverter();
    }

    public function apply(Request $request, ParamConverter $configuration) {
        $param = $configuration->getName();
        if (!$request->query->has($param)) {
            return false;
        }
        $value = $request->query->get($param);
        $request->attributes->set($param, $value);
    }
}

services.yml :

services:
  querystring_paramconverter:
    class: AppBundle\Extension\QueryStringConverter
    tags:
      - { name: request.param_converter, converter: querystring }

In your controller:

/**
 * @Route("/test")
 * @ParamConverter("name", converter="querystring")
 */
public function action(Request $request, $name) {
  return new Response(print_r($name, true));
}
2
votes

An improved solution based on Remy's answer which will map the parameter to an entity :

<?php
namespace AppBundle\Extension;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\DoctrineParamConverter;

/**
 * Put specific attribute parameter to query parameters
 */
class QueryStringConverter extends DoctrineParamConverter {

    protected function getIdentifier(Request $request, $options, $name)
    {
        if ($request->query->has($name)) {
            return $request->query->get($name);
        }

        return false;
    }

}

services.yml:

services:
  querystring_paramconverter:
    class: MBS\AppBundle\Extension\QueryStringConverter
    arguments: ['@doctrine']
    tags:
      - { name: request.param_converter, converter: querystring }

in your controller:

/**
 * @Route("/test")
 * @ParamConverter("myobject")
 */
public function action(Request $request, AnyEntity $myobject) {
  return new Response(print_r($myobject->getName(), true));
}
-1
votes

I havn't checked, but it seems that the FOSRestBundle provides the @QueryParam annotation which does that : http://symfony.com/doc/current/bundles/FOSRestBundle/param_fetcher_listener.html