我有几个路由不起作用,我不明白为什么,因为其他路由都起作用,这里是我的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/一种类型->工作
mywebsite/一种类型/一个位置->不工作
mywebsite/一个类型/一个位置/在id上->不工作
mywebsite/connection->work
mywebsite/confirm->工作
我有点困惑,因为你不能使用不同的路线类型。所以对于不起作用的路由,我也有"……无法呈现模板"布局/布局。。。"当我用与template_map键中的"layout/frontlayout"相同的路径添加"layout/layout"时,这个错误得到了解决(我也不太明白为什么,但它有效,所以如果你能启发我,我将非常感谢),或者,a"请求的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',
),
),
),
),
文字路由是稍后推送的,所以我必须确保它们在定义顶级分段的分段路由之前匹配。
我将"搜索"answers"细节"路线结合起来,使"细节"成为"搜索"的子路线。这将确保路由器不会试图匹配标识符,除非也存在位置。这应该会加快匹配速度,并稍微缩小定义。
我更改了位置约束regex以接受单词和/或数字。