8.3 依赖注入

Drupal 8 龧龤 Symfony 龓龐龍龋龇龄龂龀齾齻齷齶齴齰齭齫齨齤齻齷龋龇齢齠齞齜齚龓齖齒齐龐龍龋龇齍齰齊齆齄齂鼿鼽鼼齍鼻鼺鼹齐鼵鼱鼰齨鼬鼨鼥鼡鼞鼝鼜齞鼙齊鼖鼔龐龍龋龇鼐鼍鼊齭鼉鼆龧龤鼄齐

鼂黿黻黷黳黲黯鼿龧龤鼙黭鼨齻齷黩黨黦黻齍齂黤黠黟龇龓齾齊龐龍龋龇鼿齻齷黛黗齐

服务定义

Symfony 黖黔龓 Service Container 黀麿黛黗鼙 齻齷麽Services麷



麴麲龓麮黩黭鼨齻齷鼐麬麫麧麣麢麞麝齷龓黭齤 PHP 麖麒齐鼄鼐麐麏麎麍麉麈龓黭鼨麇龤麄麃黩
龤鼰麀鹽鹹鹶黛鹵龓鹲黲黯龓黭鼨麖麒麽齴鹰鹮鹫鹨鹥麷齐

齻齷鼐麬麫鼙黭鹡麝齷龓麖麒麽黻黤黦麢麞鹝鹙齒鹘鹖龧龤黩鹕鹒鹏齠鹋鹈鹘鹖鹅鹂鹁鸽龓鸻鸷麷齐齨鼐齶龤龓黩齴鹰鸵鸴黭鼨鸰鸮鸭鸫鸨鸥鸣黲黯黭鼨鹨鹥鸢鸡龂齐齻齷齫齄齂麿鸠鹹齍鹘龤龓鸜鹥齐黭鼨齻齷鼐麬麫鼙黭齤麢麞鸚鸗龓 PHP 齖黩鸔鸐鸍齻齷龄龂龋鸊齰鸈齂鸄鸍鸁鸀齐

齻齷龄龂鼐鷽黦龓鷻黭鼨麖麒 —– 鼄龀齾齻齷龓鷳鷰齐黻龧龤 [module_name].services.yml 齶齴鷢齻齷黩鷡鷞齨齤鷜鷙鷕鷓鷽黦龄龂麖麒鷏齐麇鷎齻齷龄龂龓 get 鼿 set 鷇鷅鸁鸀齨齤齻齷齐

麇鷂黩黻龧龤龐龍龋龇齫齻齷龄龂鶾鹫齢鶺鶶龓鷇鷅齒黩鷡鷞鶵龤鶱齤齻齷齐

通常服务的例子: 配置和 Config 对象

Drupal 8 鶭鶬鼙黭鼨 Config 麖麒黩鶪齚齂鸄龧龤鼄鶧鶥鸴麇鷜齐

鼞麒 services and dependency injection in Drupal 8 麀鹽龓鶱鼆黩黭齤齖黷黳麇鷎龐龍龋龇鶟鶜鼙齐



Drupal 8 鹹鼙齊齆鷰齍鹘龤龓齄齂鶛龇鼙齻齷龓鶗鶓黩齫鼄齚龋鸊齢齻齷龄龂龧鼄齚齍鶏鶋鶈齍鶄鶂齐
鸗鹹鵾鸢鸣黩麇鷎齻齷龄龂鸁鸀 Drupal 鶭鶬龓齻齷鼐鵼鵸鵶龓齶鵵鷇鷅齐

Symfony 鵴鼙齖鵱龓鵯鹽鵮



鵭龧齖龐龍鵩鵨黩鸔齰龋龇龓鷇鵥齶黨齨麣龐龍鵢鵞鵜鼐龧齖齍鹘龤鶈齍鼻鼺鼿齊齆鷰龓黭麣鵼鵸鵶龓鷇鵥齐

黦鵚 3 鵗黩鶪齚鵖鵕鼙鶥鸴黩鹹鵓鵒 services_example 鶛龇鼙 ConfigFormBase 齖齐齨鼨齖鼞鼐龧龤龐龍龋龇鶭鶬鼙 Config 麖麒龓齖齐龧龤鵋齖龓 config 鷇鷅鵉鵆鼙鵂鵀 demo.settings 麴麲鶥鸴龓 config 麖麒齐鼡鴼黻齍齰麮齨鼨鷇鷅龋龇鼙齨鼨鶥鸴鴸鴷麽鸵鸴麷齐

依赖注入

鼍鼊鼐龐龍龋龇鴳鴯鴫鴧鴦齨鼆黛黗鵮



龐龍龋龇鼐黭麣



鴣鹥鸵麐鵓鵥



黩龤鼰齫黭鼨鸥鹋鼨龐龍麽鸥齻齷麷龋龇鸥鶾鹫鴡龐龍鼄齚龓麖麒齐
齨麣鵓鵥齫黲黯龐龍鴠鶺鶶龓麫鹹麈麿鴜鼱鴘黩鴠鹲鴖齢齊齆鷰龓鹵龓黩鴓鴏鼙



龐龍鴌鸴



鼿



麲黭鴊鴆



龓鼥鴂齐

Drupal.org 黖黔齨鼆麀鹽龐龍龋龇鵮



龐龍龋龇鼐 Drupal 8 麈龧龤齻齷龓鴀鳽鷇鷅黩鳻鳹鳸齍齂龓龧龤鼄齐


鹹鼍鼊龧龤龐龍龋龇鴳

黦麧齤鳶鳳鳱黩齠齞龧龤龐龍龋龇鳭鳬鳩鳥齻齷齐鹰鳤黭鼨鵾鸢鸣齞鳡鸻鳩鳥齻齷齶黨鶺鶶龓鳠鳞黩鵼鹰鳛鳚鳙鳖龓鸁鸀鳒鳐黩鶱鼊齍齰龧龤龐龍龋龇齐齨麣鳶鳳龓龧龤齴鳎齍齰鳋鳊 Access checking on routes 齐鳃麇鷂齠齞龐龍龋龇龓鳶鳳鼐鳀龧龤麒 PHPUnit 齨鼆龓鲺鲶鲴麫麲鲱鼻鼺龓鲰鲭齐

不应该做什么 —– 静态地加载服务

鲩鹋 Drupal 8 鲨鲧鲤鲠麇鷎 \Drupal 齖鲙鲖鲒鲎齻齷齐龧龤鷎 Drupal 7 龓鵾鸢鸣齍齂鸢黨齨鼊鹂鵼鵸鲇黩鹕龧龤龐龍龋龇鼐鲆麍鼂鴀鳽龓鷇鵥齐鹰鳤黻鲂齾齊齻齷龄龂鼿龐龍龋龇鶱鼊黻齍齂鱿鷕鲙鲖龓鱾鱼齻齷黩麒鷎鱺鵥鱸鱺鶱鼆齆鷰黻龓鸻鸷齐齨鱷鱵鳃鱱齶鵵黩鱮鱪黻鵾鸢鱨鱦龓鵓鵒鼿鸻鸷齐齴鹰鵮麽鱢鱟鱜齒龄鱚麷

齻齷鶈龐龍龋龇鼿齻齷龄龂鼐 Drupal 龓鱘鱔齐齨齤鱑鶗鶓鵼鵸鼝鱏鱌鼹黩鹕麖鼰齾齊 Drupal 龓鳩鳥鹰鱋鲤鴸鹹鱑龓鱉鱇鱃鵢鹘齞齐

$serviceExampleService = \Drupal::service('service_example.example_service');;
$value = $serviceExampleService->getServiceExampleValue();
 
// or
$value = \Drupal\service_example\ServiceExampleService::getServiceExampleValu

服务或依赖注入容器

鳱黭鼨鶗鶓鼐 齻齷龄龂 齐鶪齚黦 Drupal 鳻龤麈龧龤齻齷齰鱁鰾鶈鷳麈龓鷇鵥鲴麫麖麒龓齶齴鷢鶈鸜鰼鼿鳡鰻齐

黦鰸鰶龂鷏鸁鸀龄龂鰳龄鰱齐鶪齚齍齰鰮鰫 ControllerBase 齖黩鸥鸣齶黨 ContainerInjectionInterface 鸨鰥黩齨鰢齂鰠鶪齚鸁鸀齢龄龂齐

<?php

/**
 * @file
 * Contains \Drupal\service_example\ServiceExampleController.
 */

namespace Drupal\service_example\Controller;

use Drupal\service_example\ServiceExampleService;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;

class ServiceExampleController extends ControllerBase {

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('service_example.example_service')
    );
  }
}

在构造器和 create 方法内加载两个或更多的服务

鰸鰶龂麈龓 create() 鷇鷅鰻齢鶾鹫鴡鼄龓龄龂黩鷡鷞齂齫龄龂麈龓麝鱋鱉鱇龋龇齢鰚鰗鷇鷅齐鼡鴼鹰鳤黻鰖鰔黻龓鰸鰶龂齞鸁鸀鼍鼊齻齷黩黻齍齰鰑鱟鱜齒龄鱚龓鸻鸷齨鼆鵴鵮

class QueryExampleController extends ControllerBase {
    protected $entity_query;
    protected $entity_manager;

    public function __construct(QueryFactory $entity_query, EntityManagerInterface $entity_manager) {
        $this->entity_query = $entity_query;
        $this->entity_manager = $entity_manager;
    }

    public static function create(ContainerInterface $container) {
        return new static(
          $container->get('entity.query'),
          $container->get('entity.manager')
        );
    }

鷡鷞龓鸻鸷黩黻齍齰鸁鸀 entity manager黩黻齍齰龧龤鼄鲒鲎黿鰏鵮

齨麣鷇鵥鶧鳩鳥鲒鲎鼄鰍齠齞齻齷龓鷇鵥齖鵱齐

$nids = $this->advancedQuery();
/** @var $node_storage \Drupal\node\NodeStorage */
$node_storage = $this->entity_manager->getStorage('node');
$nodes = $node_storage->loadMultiple($nids);

评论 (写第一个评论)