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 first thing is to install Symfony, on your computer, here is the documentation:, but you can also follow this tutorial. We suppose we’re on Linux, but there’s a similar procedure on Windows:

$ sudo curl -LsS -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.

Generating a new bundle.

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. 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. For example the tool is not updated to the correct version.

Here are some tips.

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 “” 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 network, thank you!