Remy Mellet
Remy Mellet

Reputation: 1795

Symfony2 : Automatically map query string in Controller parameter

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 ?

Upvotes: 5

Views: 6389

Answers (5)

David Kmenta
David Kmenta

Reputation: 850

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

Upvotes: 1

MGDSoftware
MGDSoftware

Reputation: 157

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

Upvotes: 1

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

Upvotes: 3

Remy Mellet
Remy Mellet

Reputation: 1795

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

Upvotes: -1

Remy Mellet
Remy Mellet

Reputation: 1795

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

Upvotes: 11

Related Questions