@ParamConverter

WARNING: You are browsing the documentation for version 2.0 which is not maintained anymore. If some of your projects are still using this version, consider upgrading.
2.0 version
Maintained Unmaintained

@ParamConverter

Usage

The @ParamConverter annotation calls converters to convert request parameters to objects. These objects are stored as request attributes and so they can be injected as controller method arguments:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;

/**
 * @Route("/blog/{id}")
 * @ParamConverter("post", class="SensioBlogBundle:Post")
 */
public function showAction(Post $post)
{
}

Several things happens under the hood:

  • The converter tries to get a SensioBlogBundle:Post object from the request attributes (request attributes comes from route placeholders -- here id);
  • If no Post object is found, a 404 Response is generated;
  • If a Post object is found, a new post request attribute is defined (accessible via $request->attributes->get('post'));
  • As for any other request attribute, it is automatically injected in the controller when present in the method signature.

If you use type hinting as in the example above, you can even omit the @ParamConverter annotation altogether:

1
2
3
4
// automatic with method signature
public function showAction(Post $post)
{
}

Built-in Converters

The bundle has only one built-in converter, the Doctrine one.

Doctrine Converter

By default, the Doctrine converter uses the default entity manager. This can be configured with the entity_manager option:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;

/**
 * @Route("/blog/{id}")
 * @ParamConverter("post", class="SensioBlogBundle:Post", options={"entity_manager" = "foo"})
 */
public function showAction(Post $post)
{
}

Creating a Converter

All converters must implement the ParamConverterInterface:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
namespace Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\ConfigurationInterface;
use Symfony\Component\HttpFoundation\Request;

interface ParamConverterInterface
{
    function apply(Request $request, ConfigurationInterface $configuration);

    function supports(ConfigurationInterface $configuration);
}

The supports() method must return true when it is able to convert the given configuration (a ParamConverter instance).

The ParamConverter instance has three information about the annotation:

  • name: The attribute name;
  • class: The attribute class name (can be any string representing a class name);
  • options: An array of options

The apply() method is called whenever a configuration is supported. Based on the request attributes, it should set an attribute named $configuration->getName(), which stores an object of class $configuration->getClass().

Tip

Use the DoctrineParamConverter class as a template for your own converters.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.