繁体   English   中英

Zend Framework 2与路由不匹配

[英]zend framework 2 not matched by routing

我有两条路线不起作用,我也不明白为什么,因为其他所有路线都可以,这里是我的module.config.php:

return array(
'controllers' => array(
    'invokables' => array(
        'FrontApp\Controller\Index' => 'FrontApp\Controller\IndexController',
        'FrontApp\Controller\User' => 'FrontApp\Controller\UserController',
    ),
),


'router' => array(
    'routes' => array(
        'confirm' => array(
            'type'    => 'Literal',
            'options' => array(
                'route'    => '/confirm',
                'defaults' => array(
                    'controller' => 'FrontApp\Controller\Index',
                    'action'     => 'confirm',
                ),
            ),
        ),
        'user' => array(
            'type'    => 'Literal',
            'options' => array(
                'route'    => '/connexion',
                'defaults' => array(
                    'controller' => 'FrontApp\Controller\User',
                    'action'     => 'index',
                ),
            ),
        ),
        'detail' => array(
            'type'    => 'Segment',
            'options' => array(
                'route'    => '/:type/:location/:id',
                'constraints' => array(
                    'type'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'location' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'id'       => '[0-9]+',
                ),
                'defaults' => array(
                    'controller' => 'FrontApp\Controller\Index',
                    'action'     => 'detail',
                ),
            ),
        ),
        'search' => array(
            'type'    => 'Segment',
            'options' => array(
                'route'    => '/:type[/:location]',
                'constraints' => array(
                    'type'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'location' => '[a-zA-Z][a-zA-Z0-9_-]*',
                ),
                'defaults' => array(
                    'controller' => 'FrontApp\Controller\Index',
                    'action'     => 'search',
                ),
            ),
        ),
        'front' => array(
            'type'    => 'Literal',
            'options' => array(
                'route'    => '/',
                'defaults' => array(
                    'controller' => 'FrontApp\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/layout/front-layout.phtml',
        'layout/frontlayout'           => __DIR__ . '/../view/layout/front-layout.phtml',
        'frontapp/index/index' => __DIR__ . '/../view/front-app/index/index.phtml',
        'error/404'               => __DIR__ . '/../view/error/404.phtml',
        'error/index'             => __DIR__ . '/../view/error/index.phtml',
    ),
    'template_path_stack' => array(
        'front' => __DIR__ . '/../view',
        'searcher' => __DIR__ . '/../view',
        'detail' => __DIR__ . '/../view',
        'user' => __DIR__ . '/../view',
        'confirm' => __DIR__ . '/../view',
    ),
),
'module_layouts' => array(
        'FrontApp' => 'layout/frontlayout',
        'BackApp' => 'layout/backlayout',
),);

所以当我尝试:

mywebsite /->工作

mywebsite / one-type->工作

mywebsite /一种类型/一种位置->不起作用

mywebsite /一种类型/一个位置/ on-id->不起作用

mywebsite / connexion->工作

mywebsite / confirm->工作

我有点困惑,因为它就像您不能使用其他路由类型一样。 因此,对于不起作用的路线,我都会遇到“ ....无法渲染模板“布局/布局...”错误,当我添加“布局/布局”时使用与“布局/前布局”相同的路径即可解决该错误”(在template_map键中(我也不太明白为什么会这样,但是它确实起作用,所以如果您能启发我,我将非常感激)),或者,“请求的URL无法通过路由进行匹配。”​​,并且针对此错误,我被卡住了,因为我看不出问题出在哪里:/

希望你能帮助:)

这里的答案:

'routes' => array(
'search' => array(
    'type'    => 'Segment',
    'options' => array(
        'route'    => '/:type[/:location]',
        'constraints' => array(
            'type'     => '[a-zA-Z][a-zA-Z0-9_-]*',
            'location' => '[a-zA-Z0-9_-]*',
        ),
        'defaults' => array(
            'controller' => 'FrontApp\Controller\Index',
            'action'     => 'search',
        ),
    ),
    'may_terminate' => true,
    'child_routes' => array(
        'detail' => array(
            'type'    => 'Segment',
            'options' => array(
                'route'    => '/:id',
                'constraints' => array(
                    'id' => '[0-9]+',
                ),
                'defaults' => array(
                    'action' => 'detail',
                ),
            ),
        ),
    ),
),
'confirm' => array(
    'type'    => 'Literal',
    'options' => array(
        'route'    => '/confirm',
        'defaults' => array(
            'controller' => 'FrontApp\Controller\Index',
            'action'     => 'confirm',
        ),
    ),
),
'user' => array(
    'type'    => 'Literal',
    'options' => array(
        'route'    => '/connexion',
        'defaults' => array(
            'controller' => 'FrontApp\Controller\User',
            'action'     => 'index',
        ),
    ),
),
'front' => array(
    'type'    => 'Literal',
    'options' => array(
        'route'    => '/',
        'defaults' => array(
            'controller' => 'FrontApp\Controller\Index',
            'action'     => 'index',
        ),
    ),
),

),

文字路由稍后会推送,因此我必须确保它们在定义顶级细分的细分路由之前匹配。

我将“搜索”和“详细”路线结合在一起,使“详细信息”成为“搜索”的子路线。 除非位置也存在,否则这将确保路由器不会尝试匹配标识符。 这将加快匹配速度,并略微缩小定义。

我更改了位置约束正则表达式以接受单词和/或数字。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM