3

을 무시하는 것, 나는 다음과 같은 경로를 사용하여 시도 :zf2 라우팅 프레임 워크 2 젠드에서 __NAMESPACE__

The requested controller could not be mapped to an existing controller class.

Controller: User(resolves to invalid controller class or alias: User)

: http://www.example.com/MyUsernameHere에 갈 때

 'default' => array(
      'type' => 'Segment', 
      'options' => array(
       'route' => '/:username[/:action]', 
       'defaults' => array(
        '__NAMESPACE__' => 'Website\Controller', 
        'controller' => 'User', 
        'action'  => 'index', 
       ), 
      ), 
      'may_terminate' => true, 
     ), 

그러나, 나는 404없는 오류를 라우터가 'Website\Controller' 네임 스페이스를 완전히 무시하고 그 앞에 네임 스페이스가없는 User을 찾습니다. 내가 수동과 같이 네임 스페이스를 입력하면

그래서 :

다음
 'default' => array(
      'type' => 'Segment', 
      'options' => array(
       'route' => '/:username[/:action]', 
       'defaults' => array(
        'controller' => 'Website\Controller\User', 
        'action'  => 'index', 
       ), 
      ), 
      'may_terminate' => true, 
     ), 

페이지가로드가 예상대로.

무엇을 제공합니까? 컨트롤러에 '__NAMESPACE__' 매개 변수를 사용할 수 있습니까? ZF2 웹 사이트는 '__NAMESPACE__'을 사용하여 분명히 example을 제공하지만 실제로 작동하지는 않습니다. 예제가 잘못되었거나 구형입니까?

답변

4

예상대로 작동하려면 ModuleRouteListener을 MVC 이벤트 관리자에 연결해야합니다. 당신은 당신의 모듈 onBootstrap 방법으로이 작업을 수행 할 수 있습니다

public function onBootstrap(MvcEvent $event) 
{ 
    //... 
    $application = $event->getApplication(); 
    $eventManager = $application->getEventManager(); 
    $moduleRouteListener = new ModuleRouteListener(); 
    $moduleRouteListener->attach($eventManager); 
    //... 
} 

예상대로 코드가 작동합니다있었습니다.

그들은 실제로 당신이 질문에서 언급 한 예제가있는 페이지에서 이것을 언급해야합니다. 모듈 경로 수신기 here in the Zend\Mvc documentation에 대한 자세한 내용을 확인할 수 있습니다. 그들은 거기에 쓸 :

This listener determines if the module namespace should be prepended to the controller name. This is the case if the route match contains a parameter key matching the MODULE_NAMESPACE constant.

+0

이 작품은; 정말 고맙습니다. 우리는 원래이 코드를 스켈레톤 응용 프로그램에서 옮겼습니다. 왜냐하면 우리는 그 목적을 이해하지 못했고 불필요한 작업을 우회하려고했기 때문입니다. –