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: 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:
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:
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!
Hi there. My name is Mirko Benedetti, I’m a Software Developer and I founded Benedict Engineers. Excellence is what I consider to be our ultimate goal, and passion for technology constantly drives me to it. I began programming self-taught at a very young age. Since then I learned a lot, and every day I enjoy learning new things. Here is my Linkedin Curriculum, feel free to connect.