在 CakePHP 中设置条件路由
Setting up conditional routing in CakePHP
我使用的是 CakePHP 2.7,我在 app/Config/routes.php
:
中定义了以下路由
Router::connect('/:state/:location/:slug',
['controller' => 'pages', 'action' => 'display_program'],
['pass'=> ['state', 'location', 'slug']]
);
Router::connect('/:state/:location/:degree_level/:slug',
['controller' => 'pages', 'action' => 'display_program'],
['pass'=> ['state', 'location', 'degree_level', 'slug']]
);
Router::connect('/:state/:location/:degree_level',
['controller' => 'pages', 'action' => 'list_programs'],
['pass'=> ['state', 'location', 'degree_level']]
);
我遇到的问题是当我传递三个值时,第三个值可以是学位级别或 slug。
/az/campus/program-name-slug [should route to pages->display_program()]
/az/campus/masters/program-name-slug [should route to pages->display_program()]
/az/campus/masters [should route to pages->list_programs()]
现在所有三个 url 都路由到 display_program() 操作,但是我需要第三个(没有 slug)路由到 list_program()。非常感谢任何想法!
因此,如果您的学位级别从不与课程名称冲突,那么您可以使用简单的正则表达式进一步缩小 degree_level
元素匹配的范围,例如
Router::connect('/:state/:location/:degree_level',
['controller' => 'pages', 'action' => 'list_programs'],
[
'pass'=> ['state', 'location', 'degree_level'],
'degree_level' => 'undergraduate|graduate|bridge|doctorate'
]
);
这样 degree_level
元素将仅在使用四个给定字符串之一的情况下匹配。
为了让它最终起作用,你必须把这条路线 放在 带有 slug
元素的路线之前,否则鼻涕虫会偷走这条路线,因为它贪婪地匹配任何字符串,即最后你应该将路由排序为
// specific count match, semi-specific value match
Router::connect('/:state/:location/:degree_level',
// ...
);
// specific count match, greedy value match
Router::connect('/:state/:location/:slug',
// ...
);
// sepcific count match, greedy value match
Router::connect('/:state/:location/:degree_level/:slug',
// ...
);
我使用的是 CakePHP 2.7,我在 app/Config/routes.php
:
Router::connect('/:state/:location/:slug',
['controller' => 'pages', 'action' => 'display_program'],
['pass'=> ['state', 'location', 'slug']]
);
Router::connect('/:state/:location/:degree_level/:slug',
['controller' => 'pages', 'action' => 'display_program'],
['pass'=> ['state', 'location', 'degree_level', 'slug']]
);
Router::connect('/:state/:location/:degree_level',
['controller' => 'pages', 'action' => 'list_programs'],
['pass'=> ['state', 'location', 'degree_level']]
);
我遇到的问题是当我传递三个值时,第三个值可以是学位级别或 slug。
/az/campus/program-name-slug [should route to pages->display_program()]
/az/campus/masters/program-name-slug [should route to pages->display_program()]
/az/campus/masters [should route to pages->list_programs()]
现在所有三个 url 都路由到 display_program() 操作,但是我需要第三个(没有 slug)路由到 list_program()。非常感谢任何想法!
因此,如果您的学位级别从不与课程名称冲突,那么您可以使用简单的正则表达式进一步缩小 degree_level
元素匹配的范围,例如
Router::connect('/:state/:location/:degree_level',
['controller' => 'pages', 'action' => 'list_programs'],
[
'pass'=> ['state', 'location', 'degree_level'],
'degree_level' => 'undergraduate|graduate|bridge|doctorate'
]
);
这样 degree_level
元素将仅在使用四个给定字符串之一的情况下匹配。
为了让它最终起作用,你必须把这条路线 放在 带有 slug
元素的路线之前,否则鼻涕虫会偷走这条路线,因为它贪婪地匹配任何字符串,即最后你应该将路由排序为
// specific count match, semi-specific value match
Router::connect('/:state/:location/:degree_level',
// ...
);
// specific count match, greedy value match
Router::connect('/:state/:location/:slug',
// ...
);
// sepcific count match, greedy value match
Router::connect('/:state/:location/:degree_level/:slug',
// ...
);