How to build a reusable and manageable symfony Bundle via Composer

Asked

Viewed 201 times

2

Even after reading the best practices for code reuse in symfony and research on the use of commiserate, I still can’t understand how in practice I should create my Barcode to be reusable. I know the way I do it is not ideal and I wanted to do it the right way, but I’m having a hard time understanding how I should do it.

Today I do it this way:

  1. I create a project using the symfony model.
composer create-project symfony/framework-standard-edition projects/symfony2 "2.8.*"
  1. I create a Bundle with the namespace I want
app/console generate:bundle --namespace=Vendor\Bundle\MeuModuloBundle --format=annotation
  1. I change the filename of the package in the Composer.json, the description and add the dependencies I need (only this) and then update Composer to install the dependencies.
composer update
  1. I initialize the Bundles of the dependencies I put in Appkernel.php

  2. I work on the business rules of my Bundle (Vendor Bundle Meumodulobundle)

  3. I eat and it is ready my Bundle.

If I need to reuse Bundle I add its name in Composer (as I described in Vendor.json (Vendor Bundle Meumodulobundle) and run the Composer update command. (Satis stores the packets)


The problem in all this is that the code goes full of garbage, with the app folder, src, the conflict with appKernel and etc... because in reality it is a treats as a project (because I did so the wrong way for what I want) I want to actually reuse it as a module, but I don’t know how to do exactly, because I need to create a module but I want to be able to use Doctrine, route, fosrest and other features that are already ready in symfony.

How should I proceed to do right?


Good practices exhibit the folder structure that must be created in order for the project to be compatible with symfony one of my questions is how would I create a clean project, for example: Would I have to create a standard structure and not use the symfony model? where would I put the config.yml configuration files, Parameter.yml, Appkernel, how would Composer.json, autoload, app.php, app_dev.php, console and etc... used today, or it would be replaced by what? that is the question in itself.

  • 1

    You are using Symfony 2.8 (you can check the code you posted from the Composer command), however the version you read about good Symfony practices (at least the link is the link that is in the question) was for the version "Current", which at the time of writing the commentary is 3.2. Symfony 2.8 version is different from 3.*, so I recommend reading the good practices for the version you are using: https://symfony.com/doc/2.8/bundles/best_practices.html

  • @Filipemoraes Thank you for the comment. The best practices exhibit the folder structure that must be created in order for the project to be compatible with symfony version 2.8 and 3.2 in this aspect does not influence much, because my doubt is how I would create a clean project, for example: Would I have to create a standard structure and not use the symfony model? where would I put the appKernel configuration files used today, or it would be replaced by what? that’s the question itself.

1 answer

1


You are very close to creating a Bundle Properly, your only problem as far as I can see is a clear notion of dependencies and where to place the files.

Dependencies

The composer will always manage your dependencies, so at the time of development, you are doing right to install everything and proceed. When to make your Bundle, you should remove the folder vendor/.

When downloading the package, in your case Satis, Poser will read the file composer.json of your Bundle and will solve any dependencies that are missing. This way, you can use any library you want, just add to composer.json.

Folder structure

The folder structure is also very simple. By default library developers put everything in the folder src/ and its sub-folders. You are free to use the structure you want.

One important point to think about is settings files and other properties. As you mentioned it loads some settings, I’ll assume you’re putting the file minha_config.yml in the briefcase app/config/minha_config.yml and that his Bundle depends on some information that are in it to work.

If this is your case, you should load the settings dynamically through a settings reader. I will not detail the whole process not to get a very extensive response, but you can see in the official documentation on this link.

Appkernel

Should your Bundle depends on others to function and, consequently, be registered in the appKernel you will have to transfer this responsibility to the users of your package.

For example: Let’s say I’m developing the TutorialBundle that depends on the NinjaProgrammingBundle. While I’m developing and testing I can register the NinjaProgrammingBundle in the appKernel.php of my Bundle hassle-free.

When to make available the TutorialBundle for other users, you should add in the documentation some information saying that you need the NinjaProgrammingBundle be registered before your.

I hope I’ve helped you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.