Mini Tutorial about the Symfony Bundle Generator

In this mini tutorial I’m gonna show you a little trick which can be useful in order to create a new bundle in Symfony.

If you choose to create a bundle yourself, there might not be problems, but if you want to use the bundle creator tool of Symfony, you may encounter some difficulties that can make you loose precious time.

The Symfony Php Framework

The first thing is to install Symfony, on your computer, here is the documentation: https://github.com/symfony/symfony-installer, but you can also follow this tutorial. We suppose we’re on Linux, but there’s a similar procedure on Windows:

$ sudo curl -LsS https://symfony.com/installer -o /usr/local/bin/symfony
$ sudo chmod a+x /usr/local/bin/symfony

The second thing is to create a new project skeleton in a directory of your choice:

$ symfony new symfony_tutorial 3.4

This command will create a new project called “my_project” using the 3.4 version of Symfony, this may take a while, you’ll just have to wait.

Now switch to your project directory and issue this command to generate automatically a new bundle:

$ php bin/console generate:bundle --namespace=YourNameSpace/DemoBundle

This command is interactive and will ask you some important questions about your bundle. You can leave the default values for now by pushing enter when asked. Now, if it all goes through like it’s supposed to, you should have a new directory under the “src” folder of your project.

It will be called “YourNameSpace”, and inside it you will find another folder called “DemoBundle”.

Look inside of it, and you will find two more folders: “Controller” in which there will be all the controllers of your bundle, and “Resources” which will host configurations and your views.

Sometimes though this tool doesn’t perform correctly his job, for various reasons (e.g. the tool is not updated to the correct version).

In those cases the bundle creator will show an error saying that you’ll have to manually update dependencies on “composer.json” on your project’s root directory; add the following line to “autoload”, “psr-4” on that file:

"YourNameSpace\DemoBundle\": "src/YourNameSpace/DemoBundle"

This problem could occour even if the procedure went smoothly but when you try to run the Symfony server you get an error, saying that it doesn’t find a class.

Remember to put a comma after the first value (i.e. “AppBundle”). Finally, go to your shell in the root directory of your project and issue this command to update dependencies:

$ composer dump-autoload

In many cases you might also have another problem, run the Symfony server and go to “http://127.0.0.1:8000/” on your browser and you may find that the framework complains about a template not found.

$ php bin/console server:run

Simply go to your bundle controller inside the following directory: “src/YourNameSpace/DemoBundle” and replace the return statement with this one:

return $this->render('@YourNameSpaceDemo/Default/index.html.twig');

You should see an “Hello World!” if you refresh the browser. And that’s it! We tackled two simple problems in creating a bundle, but we’ve only scratched the surface. You can do many more things like changing the slug of your bundle or creating a view with twig, and so on.

I hope that this tutorial was good to you and if you have any more questions, do not hesitate to contact me in person.

Did you like this post? Please share it on your preferred social networks or comment here below, thank you!

6 thoughts on “Mini Tutorial about the Symfony Bundle Generator

  1. Yes there is,
    it’s only one of the many possiblities…
    I chose to present that, because it is not very well documented on the main Symfony website.
    Plus, you have to take care of the version of Symfony you’re working on.

  2. Really rarely do I encounter a blog that’s both educative and entertaining, and let me tell you, you have hit the nail on the head. Your idea is outstanding.

  3. Hello Reyansh,
    thank you very much for your feedback, I’ll try and stick to it.

  4. Thank you نرم افزار,
    I’m really glad you appreciate my style of writing. Seems like people enjoy the way things are explained, doesn’t matter if the tutorial is even too simple.
    Keep in touch

Leave a Reply

Give me your opinion, I will be grateful.