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 个模块。路由现在应该看起来像这样:

/ 下的所有内容都应该转到前端模块摘录 /backend

/ --> 索引控制器 --> 索引操作

/controller1 --> Controller1Controller -> indexaction

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

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

现在 /backend 下的所有内容都应该路由到后端模块

/backend --> BackendIndexController --> indexaction

/backend/controller1 --> BackendController1Controller -> indexaction

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

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

我想定义固定的路线,而不是像这样的分段路线:

:模块/:控制器/:动作

我想最终得到类似的东西

/

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

/后端

/后端/后端控制器/[:action[/:id]]

我的方法如下。现在的问题是,即使是后端路由似乎也与前端模块匹配?!我要么得到一个 404

请求的 URL 无法通过路由匹配。

或者

致命错误:在 / 中找不到类“ClBackend\Controller\AnswerController”/ * / ** / * **/checklistenassistent3/vendor/ZF2/library/Zend/ServiceManager/AbstractPluginManager.php 第177行

CLFrontend/config/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/config/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',
                                    ),
                            ),
                    ),
                ),
            ),
        ),
    ),
);
4

1 回答 1

0

你考虑过控制器工厂吗?它将允许您匹配单个路由并使用逻辑来决定使用哪个控制器。

例如,您的路线可能如下所示:

'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 前添加可调用名称。

我不确定我自己会采取这条路线,但我希望这对您想要实现的目标有所帮助。

于 2012-11-22T17:37:37.780 回答