【问题标题】:Routes with multiple modules具有多个模块的路由
【发布时间】:2012-11-22 10:08:16
【问题描述】:

我目前在 ZF2 工作,需要一些帮助。 不幸的是,我只能找到为只有一个模块的情况设置路线的示例。或者该示例中仍有应用程序模块,具有动态分段路由。我想完全删除应用程序模块,只让我自己的模块在其中运行配置所有路由。

我有两个模块: CL前端, CL后端

我的应用程序配置如下所示:

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 下的所有内容都应该路由到后端模块

/backend --> BackendIndexController --> indexaction

/backend/controller1 --> BackendController1Controller -> indexaction

/backend/controller1/add --> BackendController1Controller --> 加法

/backend/controller1/add/1/ --> BackendCONtroller1Controller --> addaction --> 项目 1

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

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

我想得到类似的东西

/

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

/后端

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

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

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

或者

致命错误:找不到类“ClBackend\Controller\AnswerController” 在 //*/**/***/checklistenassistent3/vendor/ ZF2/库/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',
                                    ),
                            ),
                    ),
                ),
            ),
        ),
    ),
);

【问题讨论】:

  • " 从您的代码角度来看,没有“模块”之类的东西。模块在启动时注册资源,此后不再相关。在 zf2 中,您可以通过类或别名指定确切的控制器哪个控制器注册了 controllerManager"
  • @Xerkus 我编辑了我的问题,以澄清与上述线程的区别

标签: php zend-framework2 routes


【解决方案1】:

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

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

'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。

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

【讨论】:

    猜你喜欢
    • 2018-05-12
    • 2018-01-07
    • 1970-01-01
    • 2017-04-22
    • 1970-01-01
    • 2019-06-12
    • 1970-01-01
    • 2017-12-03
    • 2016-07-29
    相关资源
    最近更新 更多