...
In this chapter, we will create a new command, identified as ezpublish:cookbook:testhello, that takes an optionnal name argument, and greets that name. To do so, we need one thing: a class ending in Command that extends Symfony\Component\Console\Command\Command
. In your bundle's directory (src/EzSystems/CookbookBundle), create a new directory named Command, and in this directory, a new file named TestCommand.php
.
...
Code Block | ||||
---|---|---|---|---|
| ||||
<?php namespace EzSystems\CookBookBundle\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Input\InputArgument; class TestCommandHelloCommand extends \Symfony\Bundle\FrameworkBundle\Command\Command { /** * Configures the command */ protected function configure() { } /** * executeExecutes the command * @param InputInterface $input * @param OutputInterface $output */ protected function execute( InputInterface $input, OutputInterface $output ) { } } |
...
One class, with a name ending with "Command" (TestCommand
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.
...
Code Block | ||
---|---|---|
| ||
protected function configure() { $this->setName( 'ezpublish:cookbook:testhello' ); $this->setDefinition( array( new InputArgument( 'name', InputArgument::OPTIONAL, 'An argument' ) ) ); } |
First, we use setName() to set our command's name to "ezpublish:cookbook:testhello
".
We then use setDefinition()
to add an argument, named myArgument
name
, to our command.
You can read more about arguments definitions and further options in the Symfony 2 Console documentation.Once Once this is done, if you run php ezpublish/console list
, you should see ezpublish:cookbook:testhello
listed in the available commands. If you run it, it should just do nothing.
...
Code Block | ||
---|---|---|
| ||
protected function execute( InputInterface $input, OutputInterface $output ) { // fetch the input argument if ( !$name = $input->getArgument( 'myArgumentname' ) ) { $name = "World"; } $output->writeln( "Hello $name" ); } } |
Creating a custom route with a controller action
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.
Info | ||
---|---|---|
| ||
This eZ Publish 5 extension would have been a custom module, with its own |
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/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.
routing.yml
This is the file where we define our action's URL matching.
Creating and editing Content
The following recipes show how to create simple content. As we don't want to rely on a specific installation with predefined content types we first show how to create a content type group and a simple content type within this group. Then we will create a content object of the newly created content type. The last two recipes show how to create content containing images and xml text.
...