How to reuse code with symfony 5 bundles? Part 1. Minimum bundle

Let's talk about how to stop copy-paste between projects and transfer the code to a re-usable symfony 5 plug-in bundle. A series of articles summarizing my experience with bundles will lead in practice from creating a minimal bundle and refactoring a demo application to tests and the bundle release cycle.


In the first part:


  • Why are bundles needed?
  • Example Project: Calendar
  • Customize the environment: 2 ways to develop
  • Create a minimal bundle
  • Connecting a bundle to a project


What is a bundle and why is it needed?


You will need the Symfony Bundle then (and only then) when you get tired of copy-paste code from the project to the project and think about its reuse. Sooner or later, an understanding comes that it is more convenient to isolate the code into a reusable plug-in. Symfony Bundle - this is such a module in the Symfony ecosystem.


The bundle is a package of reusable PHP code on steroids Symfony Framework.

Bundles differ from regular PHP packages by using components and generally accepted conventions that simplify integration with a Symfony application. By following ecosystem-specific naming conventions and code structures, bundles can automatically connect, configure, and expand in the Symfony host application. Bundles connect to the project through the dependency manager composer.


, β€” . , DDD- .


KnpMenuBundle

Symfony: KnpMenuBundle.


, . , .


, "" , Symfony . - () , PHP-, Composer.


Example Project: Calendar


.



.


, . . , . - , .


, 10 .
: .
, , 12 .


, .


, , , . , , , composer update.


.


README.md .


:


  • Event
  • 2
  • , webpack-encore

.



, ?


β€” , File -> New... -> Symfony Project. -, .


2 :


  1. , Symfony . , .


    , Symfony
    -
    -

  2. , . , ./bundles.


    ,
    -2 ( PhpStorm )
    IDE,: ,


, .



./bundles CalendarBundle,
:


src/CalendarBundle.php
composer.json

2 !


composer.json


composer.json :


{
    "name": "bravik/calendar-bundle",
    "version": "0.1.0",
    "type": "symfony-bundle",
    "description": "Symfony bundles tutorial example project",
    "license": "proprietary",
    "require": {
        "php": "^7.3"
    },
    "require-dev": {
    },
    "config": {
        "sort-packages": true
    },
    "autoload": {
        "psr-4": {
            "bravik\\CalendarBundle\\": "src/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "bravik\\CalendarBundle\\Tests\\": "tests/"
        }
    },
    "scripts": {
        "test" : "./vendor/bin/simple-phpunit"
    },
    "extra": {
        "symfony": {
            "allow-contrib": false,
            "require": "5.0.*"
        }
    }
}

:


"name": "bravik/calendar-bundle",
"description": "Health check bundle",

. . composer, vendor/bravik/calendar-bundle


"type": "symfony-bundle"

Symfony, . composer, β€” Symfony Flex, β€” -, ( bundles.php), "".


β€” Symfony Flex, , .


"version": "0.1.0",

. composer . .


"autoload": {
    "psr-4": {
        "bravik\\CalendarBundle\\": "src"
    }
},
"autoload-dev": {
    "psr-4": {
        "bravik\\CalendarBundle\\Tests\\": "tests"
    }
},

composer. <VendorName>/<CategoryName>/<BundleName>Bundle.


composer, bravik\\CalendarBundle ./src composer.json . -, , use <BundleNamespace>/<BundleClass>.


dev-. .


"require": {
    "php": "^7.3"
},
"require-dev": {},

require require-dev prod dev . - . composer . php .


.



./src CalendarBundle.php :


<?php
namespace bravik\CalendarBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class CalendarBundle extends Bundle
{
}

. .


: <BundleName>Bundle.php, β€” Bundle. , Symfony , ( ).


,
.

bravik\CalendarBundle, composer.json. .


-


composer :


composer require bravik/calendar-bundle

, composer : .


, repositories composer.json .


, bundles/CalendarBundle:


"repositories": [
    {
        "type" : "path",
        "url" : "./bundles/CalendarBundle"
    }
],

vendor , bravik/calendar-bundle, . vendors, .


, git- :


"repositories": [
    {
        "type" : "vcs",
        "url" : "git@bitbucket.org:bravik/calendarbundle.git"
    }
],

composer git- vendors/bravik/calendar-bundle.


composer.


composer require bravik/calendar-bundle composer.json , . , config/bundles.php:


<?php

return [
    //...
    bravik\CalendarBundle\CalendarBundle::class => ['all' => true],
];

, !



-, ./bundles .gitignore , bundles/CalendarBundle : composer init. , .


, " " - - , .


, .



  • Symfony 2 : composer.json MyBundle.
  • - IDE GIT-, composer.
  • , .
  • , .

Example Project 1-bundle-mockup.


, , , DI-.


:


Part 1. The minimum bundle
Part 2. We take out the code and templates in the bundle
Part 3. Integration of the bundle with the host: templates, styles, JS
Part 4. Interface for expanding the bundle
Part 5. Parameters and configuration
Part 6. Testing, microapplication inside the bundle
Part 7 Release cycle, installation and update


All Articles