2016-09-01 4 views
1

컨트롤러 전달 - 위대한 기능이며 위젯 개념을 만들었습니다. 그러나 기본적으로 비 자체 모듈의 컨트롤러를 디스패치 할 수는 없습니다. (또한 모든 콘트롤 구성을 피하기 위해)이 문제를 해결하려면, 나는 컨트롤러 constructon에 대한 abstaract_factory 추가 :Zend2 - 다른 모듈 컨트롤러를 전달하는 방법?

namespace Engine\Mvc\Controller; 

use Zend\ServiceManager\AbstractFactoryInterface; 
use Zend\ServiceManager\ServiceLocatorInterface; 

class Autoloader implements AbstractFactoryInterface 
{ 
    public function canCreateServiceWithName(ServiceLocatorInterface $locator, $name, $requestedName) 
    { 
     return true; 
    } 

    public function createServiceWithName(ServiceLocatorInterface $locator, $name, $requestedName) 
    { 
     $class = ucfirst(str_replace('/', '\\', $requestedName)).'Controller'; 
     if (!class_exists($class)){ 
      return false; 
     } 
     return new $class; 
    } 
} 

/*In Module class*/ 
public function getControllerConfig() 
    { 
     return array(
      'abstract_factories' => array(
       'Engine\Mvc\Controller\Autoloader' 
      ), 
     ); 
    } 

그것은 잘 작동합니다. 그러나 생성자 인수가없는 호출 가능한 컨트롤러에만 해당됩니다.

/*in zfc-user/module.config.php */ 
'controllers' => array(
     'factories' => array(
      'zfcuser' => 'ZfcUser\Factory\Controller\UserControllerFactory', 
     ), 
    ), 

/*factory for controller*/ 
class UserControllerFactory implements FactoryInterface 
{ 
    /** 
    * Create controller 
    * 
    * @param ControllerManager $serviceLocator 
    * @return UserController 
    */ 
    public function createService(ServiceLocatorInterface $controllerManager) 
    { 
     /* @var ServiceLocatorInterface $serviceLocator */ 
     $serviceLocator = $controllerManager->getServiceLocator(); 

     $userService = $serviceLocator->get('zfcuser_user_service'); 
     $registerForm = $serviceLocator->get('zfcuser_register_form'); 
     $loginForm = $serviceLocator->get('zfcuser_login_form'); 
     $options = $serviceLocator->get('zfcuser_module_options'); 

     $controller = new UserController($userService, $options, $registerForm, $loginForm); 

     return $controller; 
    } 
} 

비뚤어진에서 - 내가 (예를 들어, 작곡가와 함께 설치 ZfcUser를) 변경할 수 없습니다, 모듈에서 컨트롤러를 포함 할 때 theese 컨트롤러가 아닌 호출 가능한 초기화를 가질 수 있기 때문에, 실패 할 수 있습니다 module1UserControllerFactoryZfcUser 모듈을 볼 수 없다면, UserController가 모든 인수를 필요로하기 때문에 내 abstract_factory로 컨트롤러를 구성하려고 시도하고 오류가 발생합니다.

Zend\Mvc\Controller\Plugin\Service\ForwardFactory을 스왑하여 해결할 수 있습니다. Forward를 "global"로 구성해야합니다. ControllerManager. 그러나 확실하지 않습니다. "글로벌"ControllerManager은 Zend2에 존재합니다.

질문 :

  1. 가되어 ControllerManager, 어떻게 내가이 ServiceManager의에서 얻을 수있는 글로벌?
  2. 컨트롤러 플러그인을 사용하는 다른 방법으로 비자가 모듈 컨트롤러 (비 자체 모듈 컨트롤러 구성으로 컨트롤러 구성시)?
+0

은 [링크]를 참조하십시오 (http://stackoverflow.com/questions/14180839/is-it-possible-to-forward-data-to-another-controller -action-in-zend-2) –

답변

0

질문이 사라졌습니다. 나는 바보입니다. 전달은 모든 컨트롤러 및 비 자체 컨트롤러에도 액세스 할 수 있습니다.

/*in zfc-user/module.config.php - here we have 'zfcuser' as controller pseudo */ 
'controllers' => array(
     'factories' => array(
      'zfcuser' => 'ZfcUser\Factory\Controller\UserControllerFactory', 
     ), 
), 

우리는 모든 모듈에서이 코드를이 컨트롤러를 전달할 수 있습니다 :

$widgetLogin = $this->forward()->dispatch('zfcuser', ['action' => 'login']); 

감사합니다 - 그러나 컨트롤러 의사에 의해 (컨트롤러 관리자의 이름)하지 클래스 이름으로, 예를 들어 우리는 컨트롤러 정의를 가지고있다.

UPD :

당신이 앞으로에서 컨트롤러를 액세스하는, 단지 클래스의 이름을 사용하려면 (문제는 해결에서 같은 이름 엔진 \ MVC \ 컨트롤러 \ 자동 로더와 함께 작동하는이) - 당신이 비에 별칭을 추가 할 수 있습니다 예를 들어, 모듈의 자체 컨트롤러 :

/*in module.config.php of your module*/ 
'controllers' => [ 
    'abstract_factories' => [ 
     'Engine\Mvc\Controller\Autoloader' 
    ], 
    'aliases' => [ 
     'ZfcUser/Controller/User' => 'zfcuser' 
    ] 
] 
관련 문제