Skip to content

Converting request parameters into API objects

In many cases, a request will provide a contentId or a locationId. Before using them, you will have to load an API object within your controller, for example:

1
2
3
public function listBlogPostsAction($locationId)
{
    $location = $repository->getLocationService()->loadLocation($locationId);

Thanks to the ParamConverter, you can directly have the API object at your disposal. All you have to do is:

  • For Locations:
    • In your controller's signature, typehint the variable to Location.
    • Make sure a parameter named "locationId" is provided by the request.
  • For Content items:
    • In your controller's signature, typehint the variable to Content.
    • Make sure a parameter named "contentId" is provided by the request.

Example using Locations:

1
2
3
4
5
use eZ\Publish\API\Repository\Values\Content\Location;

public function listBlogPostsAction(Location $location)
{
    // use my $location object

Tip

If you want to understand how it works, you can check Symfony's ParamConverter documentation and the pull request implementing the Repository ParamConverters.

Migrating your current application

See example pull request on the DemoBundle which provides a few concrete examples.

Read the Docs