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: corrected ref. EZP-21013

...

Then use the app/console application , with the generate:bundle command , to start the bundle generation wizard.

Code Block
languagebash
$ php ezpublish/console generate:bundle

Let's follow the instructions provided by the wardwizard. Our objective is to create a bundle named EzSystems/Bundles/CookBookBundle, located in the src directory.

Code Block
languagebash
$ php ezpublish/console generate:bundle

The wizard will first ask about our bundle's namespace. Each bundle's namespace should feature a vendor name (in our own case: EzSystems), optionally followed by a sub-namespace (we could have chosen to use Bundle), and end with the actual bundle's name, suffixed with Bundle: CookbookBundle.

...

In this chapter, we will create a new command, identified as ezpublish:cookbook:hello, that takes an optional name argument, and greets that name. To do so, we need one thing: a class with a name ending with "Command" that extends Symfony\Component\Console\Command\Command. Note that in our case, we use ContainerAwareCommand instead of Command, since we need the dependency injection container to interact with the Public API). In your bundle's directory (src/EzSystems/CookbookBundle), create a new directory named Command, and in this directory, a new file named HelloCommand.php.

...

First, we use setName() to set our command's name to "ezpublish:cookbook:hello".  We then use setDefinition() to add an argument, named name, to our command.

You can read more about arguments definitions and further options in the Symfony 2 Console documentation. Once this is done, if you run php ezpublish/console list, you should see ezpublish:cookbook:hello listed in the available commands. If you run it, it should just do nothing.

...

In this short chapter, we will see how to create a new route that will catch a custom URL and execute a controller action. We want to create a new route, /cookbook/test, that displays a simple 'Hello world' message. This tutorial is a simplified version of the official one that can be found on http://symfony.com/doc/current/book/controller.html.

...

During our bundle's generation, we have chosen to generate the bundle with default code snippets. Fortunately, almost everything we need is part of those default snippets. We just need to do some editing, in particular in two locations: src/EzSystems/CookbookBundle/Resources/config/routing.yml and src/EzSystems/CookbookBundle/Controllers/DefaultController.php. The first one will be used to configure our route (/cookbook/test) as well as the controller action the route should execute, while the latter will contain the actual action's code.

...

Code Block
titleEdited routing.yml
ezsystems_cookbook_hello:
    pattern:  /cookbook/hello/{name}
    defaults: { _controller: EzSystemsCookbookBundle:Default:hello }

...

This controller was generated by the bundle generator. It contains one method, helloAction(), that matched the YAML configuration we have changed in the previous part. Let's just rename the indexAction() method so that we end up with this code.

Code Block
languagephp
titleDefaultController::helloAction()
public function helloAction( $name )
{
    $response = new \Symfony\Component\HttpFoundation\Response;
    $response->setContent( "Hello $name" );
    return $response;
}

...

Controller actions must return a Response object that will contain the response's content, the headers, and various optional properties that affect the action's behavior. In our case, we simply set the content, using setContent(), to "Hello $name". Simple. Go to http://ezpublish5/cookbook/hello/YourName, and you should get "Hello YourName".

Info
titleThe custom EzPublishCoreBundle Controller

For convenience, a custom controller is available at eZ\Bundle\EzPublishCoreBundle\Controller. It gives you with a few commodity methods:

  • getRepository()
    Returns the Public API repository, that gives you access to the various services through getContentService(), getLocationService() and so on; 
  • getLegacyKernel()
    Returns an instance of the eZ\Publish\Core\MVC\Legacy\Kernel, that you can use to interact with the Legacy eZ Publish kernel
  • getConfigResolver()
    Returns the ConfigResolver that gives you access to configuration data.

You are encouraged to use it for your custom controllers that interact with eZ Publish.

 

With both command line scripts and HTTP routes, you have the basics you need to start writing Public API code.