How to build a reusable bundle in symfony and manageable via composer

2

Even after reading best practices for code reuse in symfony and search for the use of < a href="https://tableless.com.br/composer-para-iniciantes/"> composer , I still can not understand how in practice I should create my bundle to be reusable. I know that the way I do is not ideal and I wanted to do it right, but I'm having a hard time understanding how I should do it.

Today I do the following:

  • I create a project using the symfony template.
  • composer create-project symfony/framework-standard-edition projects/symfony2 "2.8.*"
    
  • I create a bundle with the namespace I want
  • app/console generate:bundle --namespace=Vendor\Bundle\MeuModuloBundle --format=annotation
    
  • Altero in the file composer.json the package name, description and add the dependencies that I need (only that) and after, I update the composer to install the dependencies.
  • composer update
    
  • Working in my Bundle's business rules (Vendor \ Bundle \ MyModuloBundle)

  • Comito and my bundle is ready.

  • If I need to reuse the bundle I add its name in the composer (as I have described in the Vendor \ Bundle \ MyModuloBundle composer.json and run the composer update command. p> The problem in all this is that the code goes full of garbage, with the app, src folder, the conflict with the appKernel and etc ... because in reality it is a treats like a project (because I did so in the way I want to actually reuse it as a module, but I do not know how to do it right, 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 it right?

    Good practice shows the structure of folders that must be created for the project to be compatible with symfony One of my doubts is how I would create a clean project, for example: I would have to create a default structure and not to use the symfony model? where I would put the configuration files config.yml, parameter.yml, AppKernel, how would composer.json, autoload, app.php, app_dev.php, console and etc ... used today, or would it be replaced by what ? that is the question itself.

        
    asked by anonymous 18.12.2016 / 21:13

    1 answer

    1

    You're pretty close to creating a Bundle correctly, your only problem as far as I can tell is a clear notion of dependencies and where to put the files.

    Dependencies

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

    When downloading the package, in your Satis case, the composer will read the file composer.json of its Bundle and will resolve the missing dependencies. This way, you can use any library you want by simply adding it to composer.json .

    Folder structure

    The folder structure is also very simple. By default, library developers put everything into the src/ folder and its subfolders. You are free to use the framework you want.

    An important point to think about is configuration files and other properties. As you mentioned that it loads some settings, I'll assume you're putting minha_config.yml in the app/config/minha_config.yml folder and that your Bundle depends on some information that is in it to work.

    If this is the case, you should load the settings dynamically through a settings reader. I will not detail the whole process for not getting a very extensive answer, but you can see in the official documentation in this link .

    AppKernel

    If your Bundle is dependent on others to work and consequently is registered with appKernel you will have to transfer this responsibility to users in your package.

    For example : Let's say I'm developing TutorialBundle that depends on NinjaProgrammingBundle . While I'm developing and testing I can register the NinjaProgrammingBundle in appKernel.php of my Bundle without problems.

    When you make TutorialBundle available to other users, you should add some information in the documentation stating that you require NinjaProgrammingBundle to be registered before yours.

    I hope I have helped you.

        
    07.02.2017 / 18:03