5

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 ?

5 Answers 5

11

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));
}
Sign up to request clarification or add additional context in comments.

1 Comment

This is great, but I want to bind the ParamConverter to an attribute/annotation, github.com/symfony/symfony/discussions/43960
3

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

3 Comments

Nice solution, however it breaks functionality of DoctrineParamConverter. I would add if ($id = parent::getIdentifier($request, $options, $name)) return $id on top here.
getIdenfitier is no longer protected method. It's better to extend apply function instead of it.
Sure. This solution is definitely outdated. But the question was about Symfony 2.
1

like #2, To solve private method (getIdentifier) first set attributes and execute normally (parent::apply). Tested on Symfony 4.4

<?php

namespace App\FrameworkExtra\Converters;

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

class QueryStringEntityConverter extends DoctrineParamConverter 
{
    public function supports(ParamConverter $configuration)
    {
        return 'querystringentity' == $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);

        return parent::apply($request, $configuration);
    }
}

Comments

1

Now with Symfony 6.3 it is possible thanks to query mappers: https://symfony.com/blog/new-in-symfony-6-3-query-parameters-mapper

Comments

-1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.