2
내가 젠드의 컨트롤러에서 내 모듈 구성을 검색하고 싶습니다

프레임 워크 3. 을 모듈 설정을 가져, ZF2에서이 작업을 수행 할 수있는 표준적인 방법을 사용하는 것 같다내가 검색 ZF3

$this->getServiceLocator() 

module.config.php의 구성에 액세스하십시오. 그러나 getServiceLocator() 방법이 없으므로 ZF3에서는 작동하지 않습니다.

이것을 달성하기위한 표준 방법은 무엇입니까?

+0

MyController.php

MyControllerFactory.php

<?php namespace My\Namespace; use Interop\Container\ContainerInterface; use Zend\ServiceManager\Factory\FactoryInterface; use DependencyNamespace\...\ControllerDependencyClass; // this is not a real one of course! class MyControllerFactory implements FactoryInterface { /** * @param ContainerInterface $container * @param string $requestedName * @param null|array $options * @return AuthAdapter */ public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { // Get config. $config = $container->get('configuration'); // Get what I'm interested in config. $myStuff = $config['the-array-i-am-interested-in'] // Do something with it. $controllerDepency = dummyFunction($myStuff); /*...the rest of your code here... */ // Inject dependency. return $controllerDepency; } } 

을 : 나는 ZF3 놀 시작했을 때 이런 경우에, 내가 나에게 많은 도움이 것 하나를 공유 할 수 이것에 대한 해결책이 많이 있습니다 .. 당신이 더 잘 설명 할 수 있도록 코드를 공유 할 수 있습니까? – tasmaniski

답변

1

서비스 관리자를 통해 종속성을 주입해야합니다. 기본적으로 2 개의 클래스 컨트롤러ControllerFactory을 만들어야 모든 컨트롤러가 종속성을 갖습니다.

2

tasmaniski 님이 작성한 다른 해결책이 있으므로 답변을 찾았는지 모르겠군요.

<?php 
namespace My\Namespace; 

use Zend\Mvc\Controller\AbstractActionController; 
use DependencyNamespace\...\DependencyClass; 

class MyController extends AbstractActionController 
{ 
    private $controllerDepency; 

    public function __construct(DependencyClass $controllerDepency) 
    { 
     $this->controllerDepency = $controllerDepency; 
    } 

    /*...the rest of your class here... */ 
} 
관련 문제