Table of Contents
Questions & Feedback
Found a typo or an error?
Open a ticket.
Need support or have a technical question?
Post to the user mailing-list.
Master Symfony2 fundamentals
Symfony hosting done right
@ParamConverter
@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:
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:Postobject from the request attributes (request attributes comes from route placeholders -- hereid); - If no
Postobject is found, a404Response is generated; - If a
Postobject is found, a newpostrequest 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:
// 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:
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:
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 DoctrineConverter class as a template for your own converters.






is a trademark of Fabien Potencier. All rights reserved.