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);

评论 (写第一个评论)