...
Then use the app/console application , with the generate:bundle
command , to start the bundle generation wizard
Code Block | ||
---|---|---|
| ||
$ 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 | ||
---|---|---|
| ||
$ 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
.
Code Block | ||||
---|---|---|---|---|
| ||||
Your application code must be written in bundles. This command helps you generate them easily.
Each bundle is hosted under a namespace (like Acme/Bundle/BlogBundle).
The namespace should begin with a "vendor" name like your company name, your project name, or your client name, followed by one or more optional category sub-namespaces, and it should end with the bundle name itself (which must have Bundle as a suffix).
See http://symfony.com/doc/current/cookbook/bundles/best_practices.html#index-1 for more details on bundle naming conventions.
Use / instead of \ for the namespace delimiter to avoid any problem.
Bundle namespace: EzSystems/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
.
...
This is the skeleton for a command line script.
One class , with a name ending with "Command" (HelloCommand
), that extends Symfony\Bundle\FrameworkBundle\Command\Command
, and is part of our bundle's Command namespace. It has two methods: configure()
, and execute()
. We also import several classes & interfaces with the use keyword. The first two, InputInterface
and OutputInterface
are used to 'typehint' the objects that will allow us to provide input & output management in our script.
...
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 | ||
---|---|---|
| ||
ezsystems_cookbook_hello:
pattern: /cookbook/hello/{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.
...
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 behaviourbehavior. 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.