كيفية إعادة استخدام الكود مع حزم symfony 5؟ الجزء 2. نأخذ الرمز في الحزمة

لنتحدث عن كيفية إيقاف النسخ واللصق بين المشاريع ونقل الشفرة إلى حزمة مكون إضافي symfony 5. يمكن إعادة استخدامها. ستؤدي سلسلة من المقالات التي تلخص تجربتي مع الحزم عمليًا إلى إنشاء الحد الأدنى من الحزم وإعادة صياغة التطبيق التجريبي إلى الاختبارات ودورة إصدار الحزمة.


في المقالة السابقة ، أنشأنا مجموعة صغيرة من ملفين وأدرجناها في المشروع.


في هذه المقالة:


  • رمز النقل للحزمة
  • حقن التبعية: تسجيل خدمات الحزمة في حاوية DI
  • ترحيل وحدات التحكم وإعداد التوجيه
  • آلية تحديد مسار الموارد
  • نقل القوالب إلى الحزم


إذا كنت لا استكمال تعليمي بالتتابع، ثم تحميل التطبيق من مستودع والتحول إلى فرع 1 حزمة من نموذج بالحجم الطبيعي .


تعليمات تثبيت وبدء المشروع في ملف 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