Global navigation

   Documentation Center
   eZ Studio & eZ Platform
     User Manual
     Technical Manual
   eZ Publish 4.x / legacy

eZ Publish (5.x)

eZ Publish 5.x | For eZ Platform & eZ Studio topics see Technical manual and User manual, for eZ Publish 4.x and Legacy topics see eZ Publish legacy

Versions Compared


  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: corrected according to


Code Block
namespace My\Bundle\RestBundle\Rest\Controller;
use eZ\Publish\Core\REST\Server\Controller as BaseController;
class DefaultController extends BaseController
    public function sayHello( $name )
        // @todo Implement me


Code Block
    pattern: /my_rest_bundle/hello/{name}
        _controller: myRestBundle.controller.default:sayHello
    methods: [GET]

Due to

servereZ Systems JIRA tracker
, custom REST routes must be prefixed with ezpublish_rest_, or they won't be detected correctly.

Controller action

Unlike standard Symfony 2 controllers, the REST ones don't return an HttpFoundation\Response object, but a ValueObject. This object will during the kernel run be converted, using a ValueObjectVisitor, to a proper Symfony 2 response. One benefit is that when multiple controllers return the same object, such as a Content item or a Location, the visitor will be re-used.


Do not hesitate to look into the built-in ValueObjectVisitors, in eZ/Publish/Core/REST/Server/Output/ValueObjectVisitor, for more examples.

Cache handling

The easiest way to handle cache is to re-use the CachedValue Value Object. It acts as a proxy, and adds the cache headers, depending on the configuration, for a given object and set of options.

When you want the response to be cached, return an instance of CachedValue, with your Value Object as the argument. You can also pass a location id using the second argument, so that the Response is tagged with it:

Code Block
return new CachedValue($helloValue, ['locationId', 42]);

Input parser

What we have seen above covers requests that don't require an input payload, such as GET or DELETE. If you need to provide your controller with parameters, either in JSON or XML, the parameter struct requires an Input Parser so that the payload can be converted to an actual ValueObject.


Code Block
namespace My\Bundle\RestBundle\Rest\InputParser;
use eZ\Publish\Core\REST\ServerCommon\Input\Parser\Base as BaseParser;
use eZ\Publish\Core\REST\Common\Input\ParsingDispatcher;
use My\Bundle\RestBundle\Rest\Value\Hello;
use eZ\Publish\Core\REST\Common\Exceptions;

class Greetings extends BaseParser
     * @return My\Bundle\RestBundle\Rest\Value\Hello
    public function parse( array $data, ParsingDispatcher $parsingDispatcher )
        // re-using the REST exceptions will make sure that those already have a ValueObjectVisitor
        if ( !isset( $data['name'] ) )
            throw new Exceptions\Parser( "Missing or invalid 'name' element for Greetings." );

        return new Hello( $data['name'] );