2012-11-22 76 views
0

我目前在ZF2工作,需要一些幫助。 不幸的是我只能找到例子來設置只有一個模塊的情況下的路由。或者該示例中仍然有應用程序模塊,並帶有動態段路由。我想完全刪除應用程序模塊,只有我自己的模塊運行配置所有路由。具有多個模塊的路線

我有兩個模塊: CLFrontend, CLBackend

我的應用程序的配置是這樣的:

return array(
    'modules' => array(
     'ClFrontend', 
     'ClBackend' 
    ), 
    'module_listener_options' => array(
     'config_glob_paths' => array(
      'config/autoload/{,*.}{global,local}.php', 
     ), 
     'module_paths' => array(
      './module', 
      './vendor', 
     ), 
    ), 

); 

我想有註冊我的2個自己的模塊。路由現在應該好歹是這樣的:

一切都在/應到前端模塊摘錄/後端

/ --> IndexController --> indexaction

/controller1 --> Controller1Controller -> indexaction

/controller1/add --> Controller1Controller --> addaction

/controller1/add/1/ --> COntroller1Controller --> addaction --> item 1

現在應該一切都在/後端路線的後端模塊

/backend --> BackendIndexController --> indexaction

/backend/controller1 --> BackendController1Controller -> indexaction

/backend/controller1/add --> BackendController1Controller --> addaction

/backend/controller1/add/1/ --> BackendCOntroller1Controller --> addaction --> item 1

我想定義的路線固定,而不是像段路線看起來像這樣:

:module /:controller /:action

我想要的東西落得像

/

/controller1/[:action[/:id]]

/backend

/backend/backendcontroller/[:action[/:id]]

Myapproach了以下內容。現在的問題是,即使後端路線似乎匹配前端模塊?!我要麼得到一個404

The requested URL could not be matched by routing.

或者

Fatal error: Class 'ClBackend\Controller\AnswerController' not found in //*/**/***/checklistenassistent3/vendor/ZF2/library/Zend/ServiceManager/AbstractPluginManager.php on line 177

CLFrontend /配置/ module.config.php

return array(
     'controllers' => array(
       'invokables' => array(
         'ClFrontend\Controller\Index' => 'ClFrontend\Controller\IndexController', 
         'ClFrontend\Controller\User' => 'ClFrontend\Controller\UserController', 
       ), 
     ), 
     'router' => array(
      'routes' => array(
       'home' => array(
        'type' => 'Zend\Mvc\Router\Http\Literal', 
        'options' => array(
         'route' => '/', 
         'defaults' => array(
          'controller' => 'ClFrontend\Controller\Index', 
          'action'  => 'index', 
         ), 
        ), 
       ), 
      ), 
     ), 
     'view_manager' => array(
       'display_not_found_reason' => true, 
       'display_exceptions'  => true, 
       'doctype'     => 'HTML5', 
       'not_found_template'  => 'error/404', 
       'exception_template'  => 'error/index', 
       'template_map' => array(
         'layout/layout'   => __DIR__ . '/../view/cl-frontend/layout/layout.phtml', 
         'application/index/index' => __DIR__ . '/../view/cl-frontend/index/index.phtml', 
         'error/404'    => __DIR__ . '/../view/cl-frontend/error/404.phtml', 
         'error/index'    => __DIR__ . '/../view/cl-frontend/error/index.phtml', 
       ), 
       'template_path_stack' => array(
         __DIR__ . '/../view', 
       ), 
     ), 
); 

CLBackend /配置/ module.config.php

return array(
    'controllers' => array(
      'invokables' => array(
        'ClBackend\Controller\Answer'  => 'ClBackend\Controller\AnswerController', 
        'ClBackend\Controller\AnswerGroup' => 'ClBackend\Controller\AnswerGroupController', 
        'ClBackend\Controller\Category'  => 'ClBackend\Controller\CategoryController', 
        'ClBackend\Controller\Checklist' => 'ClBackend\Controller\ChecklistController', 
        'ClBackend\Controller\Index'  => 'ClBackend\Controller\IndexController', 
        'ClBackend\Controller\Question'  => 'ClBackend\Controller\QuestionController', 
        'ClBackend\Controller\User'   => 'ClBackend\Controller\UserController', 
      ), 
    ), 
    'router' => array(
     'routes' => array(
      'backend' => array(
       'type' => 'Zend\Mvc\Router\Http\Literal', 
       'options' => array(
        'route' => '/backend', 
        'defaults' => array(
         'controller' => 'ClBackend\Controller\Index', 
         'action'  => 'index', 
        ), 
        'may_terminate' => true 
       ), 
       'child_routes' => array (
        'answer' => array(
          'type' => 'Zend\Mvc\Router\Http\Segment', 
          'options' => array(
            'route' => '/answer/:action/:id', 
            'defaults' => array(
              'controller' => 'ClBackend\Controller\Answer', 
              'action'  => 'index', 
            ), 
          ), 
        ), 
        'answergroup' => array(
          'type' => 'Zend\Mvc\Router\Http\Segment', 
          'options' => array(
            'route' => '/answergroup/:action/:id', 
            'defaults' => array(
              'controller' => 'ClBackend\Controller\AnswerGroup', 
              'action'  => 'index', 
            ), 
          ), 
        ), 
        'category' => array(
          'type' => 'Zend\Mvc\Router\Http\Segment', 
          'options' => array(
            'route' => '/category/:action/:id', 
            'defaults' => array(
              'controller' => 'ClBackend\Controller\Category', 
              'action'  => 'index', 
            ), 
          ), 
        ), 
        'checklist' => array(
          'type' => 'Zend\Mvc\Router\Http\Segment', 
          'options' => array(
            'route' => '/checklist/:action/:id', 
            'defaults' => array(
              'controller' => 'ClBackend\Controller\Checklist', 
              'action'  => 'index', 
            ), 
          ), 
        ), 
        'question' => array(
          'type' => 'Zend\Mvc\Router\Http\Segment', 
          'options' => array(
            'route' => '/question/:action/:id', 
            'defaults' => array(
              'controller' => 'ClBackend\Controller\Question', 
              'action'  => 'index', 
            ), 
          ), 
        ), 
        'user' => array(
          'type' => 'Zend\Mvc\Router\Http\Segment', 
          'options' => array(
            'route' => '/user/:action[/:id]', 
            'defaults' => array(
              'controller' => 'ClBackend\Controller\User', 
              'action'  => 'index', 
            ), 
          ), 
        ), 
       ), 
      ), 
     ), 
    ), 
); 
+0

閱讀我的答案http://stackoverflow.com/questions/12443166/zend-framework-2-default-module/12443383#12443383 – Xerkus

+0

「從代碼的角度來看,沒有'模塊'這樣的東西。在zf2中你可以通過類或者別名來指定確切的控制器,在這個控制器下,控制器註冊到controllerManager「 – Xerkus

+0

[Zend Framework 2 - 通過URL訪問多個模塊](http:// stackoverflow.com/questions/8079264/zend-framework-2-multiple-modules-by-url) – vascowhite

回答

0

您是否考慮過Controller工廠?它可以讓你匹配一條路線並使用邏輯來決定使用哪個控制器。

例如您的路由看起來是這樣:

'backend-default' => array(
    'type' => 'Segment', 
    'options' => array(
     'route' => '/:controller_type[/:action][/id]', 
     'defaults' => array(
      'action' => 'index', 
      'controller' => 'MyFactory' 
     ), 
    ), 
), 

如果這條路線被匹配此時就可以用控制器工廠(MyFactory) - 這家工廠內,您可以訪問到路由匹配參數。使用這些參數您應該能夠返回適當的控制器。

你甚至可以傳入一個額外的參數來表示它是一個後端控制器(並使用一個工廠)。

use Zend\ServiceManager\FactoryInterface; 
use Zend\ServiceManager\ServiceLocatorInterface; 

class MyFactoryController implements FactoryInterface 
{ 
    public function createService(ServiceLocatorInterface $serviceLocator) 
    { 
     // $serviceLocator is a Zend\Mvc\Controller\ControllerManager 
     $app = $serviceLocator->getServiceLocator()->get('application'); 
     $routeMatch = $app->getMvcEvent()->getRouteMatch(); 

     var_dump($routeMatch); 

     /** 
     * Create controller based off $routeMatch params 
     */ 

     return $controller; 
    } 
} 

控制器工廠將讓你的controller_type變量查找到一個有效的類名 - 或前綴可調用的名稱controller_type。

我不確定我會自己走這條路線,但我希望這對於您試圖實現的目標有點用處。