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.

...

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
titleMy/Bundle/RestBundle/Resources/config/services.yml
services:
    myRestBundle.controller.default:
        class: My\Bundle\RestBundle\Rest\Controller\Default
        parent: ezpublish_rest.controller.base

Let's create a very simple controller, that has a sayHello() method, that takes a name as an argument.

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;
 
class DefaultDefaultController extends BaseController
{
    public function sayHello( $name )
    {
        // @todo Implement me
    }
}

...

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 ( !isset( $data['name'] ) )
            throw new Exceptions\Parser( "Missing or invalid 'name' element for Greetings." );


        return new Hello( $data['name'] );
	}
}
Code Block
titleMy/Bundle/RestBundle/Resources/config/services.yml
services:
    myRestBundle.controller.default:
        class: My\Bundle\RestBundle\Rest\Controller\Default
        parent: ezpublish_rest.controller.base

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