如何在symfony 5 bundle中重用代码?第2部分。我们取出捆绑包中的代码

让我们谈谈如何停止项目之间的复制粘贴并将代码转移到可重复使用的插件包Symfony 5中。在实践中总结了一系列关于包的经验的文章,将从创建最小的包,重构演示应用程序到测试以及包发布周期的实践中进行。


上一篇文章中,我们创建了两个文件的最小捆绑并将其包含在项目中。


在这篇文章中:


  • 将代码移植到捆绑
  • 依赖注入:在DI容器中注册捆绑服务
  • 迁移控制器并设置路由
  • 资源路径确定机制
  • 将模板传输到分发包


如果您没有按顺序完成本教程,请从存储库下载该应用程序,然后切换到1-bundle-mockup分支


有关在文件中安装和启动项目的说明README.md


您可以在2-basic-refactoring分支中找到本文代码的最终版本


让我们继续重构。


我们移动主文件


捆绑软件可以包含与常规Symfony应用程序相同的所有内容:实体,控制器和命令,模板,资产,测试以及任何其他代码。

, , bundles/CalendarBundle/src ( ):


# Crate dirs
cd bundles/CalendarBundle/src/
mkdir Controller Entity Service
cd ../../../src

# Move files
mv Form Repository ../bundles/CalendarBundle/src/
mv Controller/EditorController.php Controller/EventController.php ../bundles/CalendarBundle/src/Controller
mv Entity/Event.php ../bundles/CalendarBundle/src/Entity
mv Service/EventExporter ../bundles/CalendarBundle/src/Service/EventExporter
mv Twig ../bundles/CalendarBundle/src/Twig


, . namespace use App\ bravik\CalendarBundle\.


App\: .


IDE .
IDE PhpStorm Ctrl/Cmd + Shift + R.


use App\Repository\EventRepository SiteController



.


:



SiteController : EventRepository. autowiring Symfony typehints DI-.


src/Services , .


Dependency Injection


, DI- , .

, .


, Symfony- services.yaml, DI-.


src config/services.yaml:


parameters:
    #     

services:

    #         
    _defaults:

        #      
        #     typehints   (  )
        # https://symfony.com/doc/current/service_container.html#the-autowire-option
        autowire: true

        #   :
        #       
        # https://symfony.com/doc/current/service_container.html#the-autoconfigure-option
        autoconfigure: true 

    #       DI-
    bravik\CalendarBundle\Repository\EventRepository: ~
    bravik\CalendarBundle\Controller\EventController: ~
    bravik\CalendarBundle\Controller\EditorController: ~

3 .


- config/services.yaml , .


@todo :


#  Twig   
bravik\CalendarBundle\Twig\TwigRuDateFilter: ~

#     EventExporter  DI-
bravik\CalendarBundle\Service\EventExporter\:
    resource: '../src/Service/EventExporter/*'

#  ExporterProvider   DI-
#    2   
bravik\CalendarBundle\Service\EventExporter\ExporterManager:
    arguments:
        $exporters:
            - '@bravik\CalendarBundle\Service\EventExporter\Exporters\GoogleCalendarExporter'
            - '@bravik\CalendarBundle\Service\EventExporter\Exporters\ICalendarExporter'

, , . , . src DependencyInjection/CalendarExtension.php:


<?php
namespace bravik\CalendarBundle\DependencyInjection;

use Exception;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;

class CalendarExtension extends Extension
{
    public function load(array $configs, ContainerBuilder $container)
    {
    }
}

. <BundleName>Extension, src/DependencyInjection Symfony\Component\DependencyInjection\Extension\Extension.


Symfony DI-, Extension-. , Extension::load() .

, . , PHP-. .


services.yaml :


public function load(array $configs, ContainerBuilder $container)
{
    $loader = new YamlFileLoader(
        $container,
        new FileLocator(__DIR__.'/../../config')
    );
    $loader->load('services.yaml');
}

, :



.



Symfony :


config/routes.yaml              #   
config/routes/annotations.yaml  #   ,
                                #    

. annotations.yaml:


controllers:                          #  
    type: annotation                  #   
    resource: ../../src/Controller/   #   ,  

: config/routes.yaml.


calendar_routes:
    type: annotation
    resource: '../src/Controller/'

config/routes/annotations.yaml.


:


calendar_bundle:
    resource: '@CalendarBundle/config/routes.yaml'

.
@CalendarBundle — .


Symfony .


, /admin - : /admin ?


, :


calendar_bundle:
    resource: '@CalendarBundle/config/routes.yaml'
    prefix:   /admin
    name_prefix: cms.

security . , .


, Symfony vendor_name_. .



, Twig «» .

:


  • @<BundleName>Bundle/path/to/config
  • @<BundleName>/path/to/template — .

Symfony « ». . .


./src. , Symfony 4 src/Resources. 4 Symfony , — . , .


CalendarBundle getPath():


public function getPath(): string
{
    return dirname(__DIR__);
}

@CalendarBundle .


, !



? .


. , , -.



templates templates/event :


mkdir bundles/CalendarBundle/templates
mv templates/event/* bundles/CalendarBundle/templates

— , . .


«  » IDE . event/ @Calendar/.



@Calendar Twig , namespace Twig. templates , Symfony namespace, templates Resources/views ( ).

site/index.html.twig twig-. :



, — .


: base.html.twig -:


{% extends 'base.html.twig' %}

.



, , DI-. Example Project 2-basic-refactoring.


  •  , —   namespace   .       App
  • , — Extension-.     DI-     . ,   .
  •   .   .   .
  •   ,    ,  ..,   «»  ,   .      .

, JS -.


:


1.
2.
3. : , , JS
4.
5.
6. ,
7. ,


All Articles