...
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.
...
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 | ||||
---|---|---|---|---|
| ||||
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 | ||
---|---|---|
| ||
For convenience, a custom controller is available at eZ\Bundle\EzPublishCoreBundle\Controller. It gives you with a few commodity methods:
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.