2016-07-20 5 views
1

저는 ZF3에 있는데, zend-mvc-skeleton을 사용하고 가능한 한 많은 URL과 일치 할 일반 경로를 구성하려고합니다. 물론 새로운 컨트롤러를 만들 수 있으며 (액션 메소드 포함) 즉시 사용할 수 있습니다.ZF3 zend-mvc 하나의 모듈에있는 많은 컨트롤러의 일반 경로가 작동하지 않습니다.

설명서에 설명 된 일반적인 접근 방법은 컨트롤러와 동작 (ZF2와 동일)과 일치하는 경로를 작성하는 것입니다.

여기 내 module.config.php

namespace Application; 

use Zend\Router\Http\Literal; 
use Zend\Router\Http\Segment; 
use Zend\ServiceManager\Factory\InvokableFactory; 

return [ 
    'router' => [ 
     'routes' => [ 
      'home' => [ 
       'type' => Literal::class, 
       'options' => [ 
        'route' => '/', 
        'defaults' => [ 
         'controller' => Controller\IndexController::class, 
         'action'  => 'index', 
        ], 
       ], 
      ], 
      'default' => [ 
       'type' => Segment::class, 
       'options' => [ 
        'route' => '/application[/:controller[/:action]]', 
        'defaults' => [ 
         'controller' => Controller\IndexController::class, 
         'action'  => 'index', 
        ], 
        'constraints' => [ 
         'controller' => '[a-zA-Z][a-zA-Z0-9_-]*', 
         'action'  => '[a-zA-Z][a-zA-Z0-9_-]*', 
        ], 
       ], 
      ], 
     ], 
    ], 
    'controllers' => [/* ... */], 
    'view_manager' => [/* ... */], 
], 

그것은 http://localhost/http://localhost/application/module/Application/src/IndexController.php 파일 내부의 IndexController 클래스의 indexAction() 함수를 호출에 대한 매력처럼 작동합니다.

그러나 동일한 컨트롤러 (예 : IndexController)에서 fooAction() 기능을 사용하려고하면 작동하지 않습니다. 올바르게 해결되지 않았습니다 http://localhost/application/foo. 나는 barControllerfooAction()를 얻을 수 http://localhost/bar/foo을하려고하면

A 404 error occurred 

Page not found. 

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

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

No Exception available 

같은 오류 : 나는 다음과 같은 오류가 발생합니다.

이 문제가 무엇이 있는지 알고 계십니까? 어떤 도움을 주시면 감사하겠습니다. 많은 감사합니다.

답변

1

URL의 /foo은 작업이 아닌 컨트롤러와 일치하므로 http://localhost/application/foo 경로는 fooAction()으로 해석되지 않습니다. 해당 경로를 설정하면 http://localhost/application/index/foo을 방문해야합니다.

설정을 마치려면 설정에서 컨트롤러의 별칭을 지정해야합니다. 가정, 당신은 :

'controllers' => [ 
    'invokables' => [ 
     'Application\Controller\Index' => \Application\Controller\IndexController::class 
    ] 
], 

그 다음이 경로 매개 변수와 일치하도록 컨트롤러 별칭 : 당신은 사용하여 등록되지 않은 각 컨트롤러에 대한 경로 매개 변수와 일치하는 별명을 추가해야합니다

'controllers' => [ 
    'invokables' => [ 
     'Application\Controller\Index' => \Application\Controller\IndexController::class 
    ], 
    'aliases' => [ 
     'index' => 'Application\Controller\Index' 
    ] 
], 

을 원하는 문자열, 예를 들어 컨트롤러 Namespace\Controller\BarControllerbar 등으로 별명을 지정해야합니다.

+0

오타가 잘못 작성되어 죄송합니다. 언급 한대로'http : // localhost/application/index/foo'라고 작성해야합니다. "오류"는 누락 된 별명에서 비롯된 것입니다. 이제 작동 중입니다. 고마워. –

+0

다른 디렉토리에서이 zend 응용 프로그램을 사용하고 있습니다. 그래서 나는'http : // localhost/zend/skeleton-application/public/application/index/test'를 시도했다. 하지만 아직도 404가 나와 있네. 내가 놓친 곳에서 나를 칠할 수 있니? –

+0

나는 골격 응용 프로그램과 함께 제공되는 파일 중 하나를 사용하고 있다고 가정 할 때 서버 구성에 문제가 있다고 생각합니다. 공개 디렉토리 내의 파일을 검사하고 다른 모든 것을 다시 index.php에 다시 써야합니다. 나는 당신이 문서 루트의 하위 디렉토리 안에 zf를 가지고 있다는 사실이 ZF3의 라우팅을 엉망으로 만든다고 생각한다. 새로운 질문으로 게시하는 것이 좋습니다. – avy

0

나는 비슷한 문제를 가지고 왔습니다. "Application"모듈에 두 개의 컨트롤러를 만들고 같은 이름을 가진 두 개의 새 모듈 "Account"에 두 개의 컨트롤러를 만들었습니다. 여기

Application/Controller/IndexController 
Application/Controller/OverviewController 
Account/Controller/IndexController 
Account/Controller/OverviewController 

내 modules.config.php 있습니다

module/Account/config/module.config.php

return [ 
'router' => [ 
    'routes' => [ 
     'Account-account' => [ 
     'type' => Segment::class, 
     'options' => [ 
      'route' => '/account[/][:controller[/][:action][/]]', 
      'defaults' => [ 
      '__NAMESPACE__' => 'Account\Controller', 
      'controller' => Account\Controller\IndexController::class, 
      'action'  => 'index', 
      'locale' => 'en_us' 
      ], 
     ], 
     'may_terminate' => true, 
     'child_routes' => [ 
      'wildcard' => [ 
      'type' => 'Wildcard' 
      ], 
     ], 
     ], 
    ], 
], 
'controllers' => [ 
    'factories' => [ 
     Controller\IndexController::class  => AccountControllerFactory::class, 
     Controller\OverviewController::class => AccountControllerFactory::class, 
    ], 
    'aliases' => [ 
     'index' => IndexController::class, 
     'overview' => OverviewController::class 
    ] 

], 

및 별칭 섹션 인 경우이 구성 내 모듈/응용 프로그램/설정/module.config.php

return [ 
    'router' => [ 
     'routes' => [ 
      'home' => [ 
       'type' => Literal::class, 
       'options' => [ 
        'route' => '/', 
        'defaults' => [ 
         'controller' => Controller\IndexController::class, 
         'action'  => 'index', 
        ], 
       ], 
      ], 
      'Application-application' => [ 
      'type' => Segment::class, 
      'options' => [ 
       'route' => '/application[/][:controller[/][:action][/]]', 
       'defaults' => [ 
       '__NAMESPACE__' => 'Application\Controller', 
       'controller' => Application\Controller\IndexController::class, 
       'action'  => 'index', 
       'locale' => 'en_US' 
       ], 
      ], 
      'may_terminate' => true, 
      'child_routes' => [ 
       'wildcard' => [ 
       'type' => 'Wildcard' 
       ], 
      ], 
      ], 
     ], 
    ], 
    'controllers' => [ 
     'factories' => [ 
      Controller\IndexController::class => IndexControllerFactory::class, 
      Controller\OverviewController::class => IndexControllerFactory::class, 
     ], 
     'aliases' => [ 
      'index'  => IndexController::class, 
     'overview' => OverviewController::class, 
     ] 

    ], 

잘못된 컨트롤러 또는 별칭 (색인/개요)이 있다는 오류 메시지가 있습니다. 별칭이있는 경우 route : "application/overview/index"가 계정 모듈로 이동합니다.