Create Symfony Bundles on a whim

Stefan Wessel
2 min readJan 14, 2021

--

German version on netbrothers.de

Everybody wants their code to be reusable. Do you want your code to be reusable? Sure you do.

If you are using the PHP Framework Symfony you’ll find that creating code for your team or the Open Source Community is best done using Symfony Bundles.

This tutorial introduces an existing Symfony Bundle for you to inject in your existing project to help you create even more easily bundled, distributed and reused Symfony code for your use-case.

So say goodbye creating all the necessary files for a new bundle, each and every time, just for you to slip in a copy & paste bug.

Introducing the NetBrothers-Create-Symfony-Bundle-Bundle. Check it out on GitHub and use composer and Packagist to include it in your project.

This bundle will add a command to your project to create a new symfony bundle.

HOW DOES IT WORK?

php composer.phar require netbrothers-gmbh/nbcsb-bundle

Composer ist PHPs package manager. More information on composer here.

The next step is executing your brand-new cli:

php bin/console netbrothers:make-bundle [YourBundleName]

Apply your own naming conventions and replace YourBundleName with it.

… thats it. Your new bundle can be used immediately.

WHAT HAPPENED?

The command created the new bundle in your project-root in the src/ directory. It also added a new entry to config/bundles.php and saved a backup in bundles.backup . Check this image to see which files and directories where created.

This is what netbrothers-gmbh/nbcsb-bundle generates for you.

Your new bundle has the namespace App\YourBundleName and because of the App prefix, it is usable right away. You can start coding.

EXCHANGE TEMPLATES?

Sure! The install contains default templates. Check the installation/ folder.

You can copy these into your project and adjust them any way you like.

Copy the installation/config/packages/netbrothers_nbcsb.yaml file to your config/packages directory. Adjust the path in the file according to your project.

To apply these changes you’ll need to clear Symfonys cache using:

php bin/console cache:clear

That’s it. You’re done.

— — — — — — — — — — — — — — — — — — — —

Visit netbrothers.de/blog for more or contact us info@netbrothers.de.

--

--