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.

...

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/resourcesResources/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.

...

This is the file where we define our action's URL matching.  The generated file contains this YAML block

Code Block
titleGenerated routing.yml
ez_systems_cookbook_homepage:
    pattern:  /hello/{name}
    defaults: { _controller: EzSystemsCookbookBundle:Default:index }

We can safely remove this default code, and replace it with this

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

We define a route that matches the URI /cookbook/*, and executes the action hello in the Default controller of our bundle. The next step is to create this method in the controller.

DefaultController.php

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;
}

We won't go into details about controllers in this cookbook, but let's detail a bit what we do here.

This method receives the parameter defined in routing.yml. It is named "name" in the route definition, and must be named $name in the matching action. Since the action is named "hello" in routing.yml, the expected method name is helloAction.

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 behaviour. 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".

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

Creating and editing Content

...