In the first part of this tutorial, we learned how to create a new Symfony project using Composer. In this article, we'll learn how to create a new bundle and add an action to the generated controller.
Symfony includes a console command that allows us to do several things for our project. The console command is located inside our Symfony project's app directory. Try to run the command from your command line to see the available console commands.
$ php Symfony/app/console
You should then see output like so
There's a lot of available commands but for this tutorial, we will focus on the generators. Let's now try to create a new bundle.
The command to use is
$ php Symfony/app/console generate:bundle
The whole process would look something like this
You will be prompted to provide some information about your bundle. In the above output, I used Aldous/DemoBundle as the bundle's namespace. I used the suggested Bundle name and Target directory.
For the configuration format, I prefer to use annotation so I entered that. Normally you would want to generate the whole directory structure so answer yes to that question. Finally, answer yes to the next questions and we now have our new bundle.
A default controller was added to our project. The file is in
Symfony/src/Aldous/DemoBundle/Controller/DefaultController.php
The contents of which, are as follows
Let's add a new action to this controller to print the sum of two numbers.
Point your browser to
http://localhost/symfonytutorial/Symfony/web/app_dev.php/sum/12/30
And you should see the sum of 12 and 30. :)