Global navigation

   Documentation Center
   eZ Studio & eZ Platform
     User Manual
     Technical Manual
     Glossary
   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

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Updated based on Marcos' comments (https://jira.ez.no/browse/EZP-20829?focusedCommentId=78456&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-78456)

...

To create a REST controller, you need to extend the ezpublish_rest.controller.base service, as well as the eZ\Publish\Core\REST\Server\Controller class.

Code Block
titlesrc/My/Bundle/RestBundle/Resources/config/services.yml
services:
    myRestBundle.controller.default:
        class: My\Bundle\RestBundle\Rest\Controller\Default
        parent: ezpublish_rest.controller.base

...

Next, you need to create the REST route. We need to define the route's controller as a service since our controller was defined as such.

Code Block
titlesrc/My/Bundle/RestBundle/Resources/config/routing_rest.yml
myRestBundle_hello_world:
    pattern: /my_rest_bundle/hello/{name}
    defaults:
        _controller: myRestBundle.controller.default:sayHello
    methods: [GET]

...

Code Block
languagephp
titleMy/Bundle/RestBundle/Rest/Controller/DefaultDefaultController.php
namespace My\Bundle\RestBundle\Rest\Controller;

use eZ\Publish\Core\REST\Server\Controller as BaseController;
use My\Bundle\RestBundle\Rest\Controller\Values\Hello as HelloValue;

class DefaultDefaultController extends BaseController
{
    public function sayHello( $name )
    {
        return new HelloValue( $name );
    }
}

...

Let's create the service for our ValueObjectVisitor first.

Code Block
titlesrc/My/Bundle/RestBundle/Resources/config/services.yml
services:
    myRestBundle.value_object_visitor.hello:
        parent: ezpublish_rest.output.value_object_visitor.base
        class: My\Bundle\RestBundle\Rest\ValueObjectVisitor\Hello
        tags:
            - { name: ezpublish_rest.output.value_object_visitor, type: My\Bundle\RestBundle\Rest\Values\Hello }

...

First, we need to create a service with the appropriate tag in services.yml.

Code Block
titlesrc/My/Bundle/RestBundle/Resources/config/services.yml
services:
    myRestBundle.input_parser.Greetings:
        parent: ezpublish_rest.input.parser
        class: My\Bundle\RestBundle\Rest\InputParser\Greetings
        tags:
            - { name: ezpublish_rest.input.parser, mediaType: application/vnd.ez.my.Greetings }

The mediaType attribute of the ezpublish_rest.input.parser tag maps our Content Type to the input parser.

...

Code Block
languagephp
titleMy/Bundle/RestBundle/Rest/InputParser/Greetings.php
namespace My\Bundle\RestBundle\Rest\InputParser;
 
use eZ\Publish\Core\REST\Server\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 ( !array_key_existsisset( $data['name',] $data ) )
            throw new Exceptions\Parser( "Missing or invalid 'name' element for Greetings." );


        //return thenew root level of the parsed structure is ignored
        $name = Hello( $data['name']  
        return new Hello( $name );
	}
}

Do not hesitate to look into the built-in InputParsers, in eZ/Publish/Core/REST/Server/Input/Parser, for more examples.