ZF2 -尝试创建一条像ZF1一样的路线



我实际上有2个模块(应用程序和管理)在我的ZF2应用程序,我想要一个url路由像在ZF1。我目前有以下路由:

'router' => array
(
    'routes' => array
    (
        'admin' => array
        (
            'type' => 'Segment',
            'options' => array
            (
                'route' => 'admin/[:controller[/:action]]',
                'constraints' => array
                (
                    'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                ),
                'defaults' => array
                (
                    '__NAMESPACE__' => 'AdminController',
                    'controller' => 'Index',
                    'action' => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array
            (
                'wildcard' => array
                (
                    'type' => 'Wildcard'
                )
            )
        ),
    ),
),

所以它将匹配"/admin", "/admin/controller", "/admin/controller/action",但不匹配"/controller/action"。

现在我需要一个到Application模块的路由。问题是,如果我只是在模块应用程序中使用这样的路由,这个新路由将匹配"/admin/controller"为controller = "admin"和action = "controller"。

我还在应用程序中尝试了以下regex路由:

'application' => array
        (
            'type' => 'Regex',
            'options' => array
            (
                'regex' => '/(?<controller>^(?!(admin)$)[a-zA-Z][a-zA-Z0-9_-]*)?' . 
                            '(/[a-zA-Z][a-zA-Z0-9_-]*)?',
                'spec' => '/%controller%/%action%',
                /*'constraints' => array
                (
                    //The controller cannot be "admin"
                    'controller' => '^(?!(admin)$)[a-zA-Z][a-zA-Z0-9_-]*',
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                ),*/
                'defaults' => array
                (
                    '__NAMESPACE__' => 'ApplicationController',
                    'controller' => 'Index',
                    'action' => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array
            (
                'wildcard' => array
                (
                    'type' => 'Wildcard'
                )
            )
        ),

但是它没有得到变量"controller"one_answers"action"

谁有解决这个问题的建议?

注意路由顺序:路由是使用后进先出堆栈处理的,所以在匹配请求url时,数组中最后出现的是最先出现的。

这意味着你必须首先定义最通用的路由,以防止它们匹配相似但更具体的路由。

使用以下顺序的

不需要对controller参数进行任何约束,因为以/admin开头的任何内容都将首先匹配

'route1' => array(
    'type' => 'Segment',
    'options' => array(
        'route' => '/[:controller[/:action]]',
        'defaults' => array (
            'controller' => 'controller',
            'action' => 'index',
        ),
    ),
),
'route2' => array(
    'type' => 'Segment',
    'options' => array(
        'route' => '/admin[/:controller[/:action]]',
        'defaults' => array (
            'controller' => 'admin-controller',
            'action' => 'index',
        ),
    ),
),

此外,您总是可以使用priority属性显式地指定路由优先级(不应该在options数组下定义,而是在路由的最顶层数组中定义),因此以下代码相当于前面的示例:

'route2' => array(
    'type' => 'Segment',
    'options' => array(
        'route' => '/admin[/:controller[/:action]]',
        'defaults' => array (
            'controller' => 'admin-controller',
            'action' => 'index',
        ),
    ),
),
'route1' => array(
    'type' => 'Segment',
    'options' => array(
        'route' => '/[:controller[/:action]]',
        'defaults' => array (
            'controller' => 'controller',
            'action' => 'index',
        ),
    ),
    'priority' => -1,
),

最新更新