【问题标题】:Reverse Routing in cakePHP 2cakePHP 2 中的反向路由
【发布时间】:2016-09-20 13:40:37
【问题描述】:
我在 cake 2.x 中使用像
这样的字符串创建了 URL
$this->Html->link('View', '/posts/view/' . $id)
//posts/view/id
在多次然后后来决定在URL中应该将/posts称为/articles。
//文章/视图/id
我不想更改现有代码,我想使用反向路由。
我阅读了有关反向路由的信息,但找不到任何合适的示例
与我的问题有关。
如果有人就我的问题给我解决方案,我们将不胜感激?
【问题讨论】:
标签:
php
cakephp
routing
cakephp-2.x
【解决方案1】:
不要对 url 进行硬编码,而是使用数组
$this->Html->link('View', array('controller' => 'posts', 'action' => 'view' ,$id));
并且在你的路由文件(app/Config/routes.php)中,指定路由规则
//The indexaction
Router::connect(
'/articles',
array('controller' => 'posts', 'action' => 'index')
);
//The view action
Router::connect(
'/articles/:id',
array('controller' => 'posts', 'action' => 'view'),
array(
'pass' => array('id'),
'id' => '[0-9]+'
)
);