We are going to create a new bundle with the generate:bundle
command using the IDE's Run Symfony2 Command dialog box. So let's try that...
1. In the Projects pane, right-click on the Projects node, and from the context menu choose Symfony2 | Run Command... to launch the Run Symfony2 Command dialog box, as follows:
You will be able to see a list of the available commands in the Matching Tasks box. You can add parameters for those commands and see the complete command in the Command dialog box.
2. From the preceding dialog box, select the
generate:bundle
command and click on Run, or double-click on the listed name to run the command. The IDE's graphical console opens for prompting the namespace.3. Enter the value for Bundle namespace, say
Application/FooBundle
.4. Enter the value for Bundle name or press Enter to accept the default bundle name as
ApplicationFooBundle
.5. Press Enter at the
Target
directory to accept the default bundle path as/src
.6. You may enter the value for Configuration format
(yml, xml, php
, orannotation)
asyml
; the default value isannotation
.7. Enter Yes for Do you want to generate the whole directory structure [no]? to generate a whole directory structure for the bundle; the default is no.
8. Confirm the bundle generation by entering Yes again.
9. At Confirm automatic update of your Kernel [yes]? and Confirm automatic update of the Routing [yes]?, press Enter to accept the default value, which is yes. So the bundle can be registered in the Symfony kernel and the bundle routing file is linked to the default routing configuration file.
10. Now, as you can see, a new bundle has been created inside the
/src
directory; thebundle
directory structure looks similar to the following:Note that the default controller, route file, template, and so on have been created simultaneously with the bundle creation.
11. Now, to test your bundle, point your browser to
http://localhost/symfony2/web/app_dev.php/hello/tonu
, and you may see an output similar to Hello Tonu!12. Check out the bundle routing file at
/src/Application/FooBundle/Resources/config/routing.yml
, and you will see the URL with the pattern/hello/{name}
mapped with the default controller's index action (ApplicationFooBundle:Default:index
). In this example, the action displays the name passed as the URL parameter in place of{name}
.
Each bundle is hosted under a namespace (such as Acme/Bundle/BlogBundle
or Acme/BlogBundle)
. The namespace should begin with a "vendor" name, such as your company name, your project name, or your client name, followed by one or more optional category subnamespaces, and it should end with the bundle name itself (which must have Bundle
as a suffix).
Note
See http://symfony.com/doc/current/cookbook/bundles/best_practices.html#index-1
for more details on bundle naming conventions.
We have seen the interactive console that asks for arguments and creates the entire bundle
directory structure automatically. Also, it registers the bundle into Symfony's /app/AppKernel.php
, and links the bundle routing configuration file into default/app/config/routing.yml
.