【问题标题】:ZF2 Child routes not workingZF2 子路由不工作
【发布时间】:2014-03-06 12:28:11
【问题描述】:

阅读了大量关于路由的文章后,我仍然无法让它工作。

当我这样做时,我可以转到“/portal”:

'portal' => array(
    'type' => 'Literal',
        'options' => array(
            'route' => '/portal',
            'defaults' => array(
                'controller' => 'Portal\Controller\Activities',
                'action' => 'index',
            ),
    ),
),

但是当我像这样添加 child_routes 时:

'portal' => array(
    'type' => 'Literal',
        'options' => array(
            'route' => '/portal',
            'defaults' => array(
                'controller' => 'Portal\Controller\Activities',
                'action' => 'index',
            ),
    ),
    'may_terminate' => true,
    'child_routes' => array(
        'default' => array(
            'type' => 'Segment',
            'options' => array(
                'route' => '[:controller[/:action]]',
                'constraints' => array(
                    'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*'
                ),
                'defaults' => array(
                    '__NAMESPACE__' => 'Portal\Controller',
                    'action' => 'index'
                ),
            ),
        ),
    ),
),

我仍然可以转到“/portal”,但是当我转到“/portal/activities/index”(相同)时,我得到“找不到页面”。

希望有人能帮忙

提前致谢!

【问题讨论】:

  • 你到底想做什么。从 url 参数创建一个匹配特定控制器和操作的路由?
  • 我想创建一个带有模块和可选控制器和操作的路由。

标签: php routing zend-framework2


【解决方案1】:

段定义不正确,缺少一个/,所以完整的子路由当前是/portal[:controller[/:action]]

改成:

'portal' => array(
    'type' => 'Literal',
        'options' => array(
            'route' => '/portal',
            'defaults' => array(
                'controller' => 'Portal\Controller\Activities',
                'action' => 'index',
            ),
    ),
    'may_terminate' => true,
    'child_routes' => array(
        'default' => array(
            'type' => 'Segment',
            'options' => array(
                'route' => '/[:controller[/:action]]',
                'constraints' => array(
                    'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*'
                ),
                'defaults' => array(
                    '__NAMESPACE__' => 'Portal\Controller',
                    'action' => 'index'
                ),
            ),
        ),
    ),
),

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多