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

评论 (写第一个评论)