cakePHP 2中的反向路由

时间:2016-09-20 13:40:37

标签: php cakephp routing cakephp-2.x

我使用像

这样的字符串在cake 2.x中创建了URL
$this->Html->link('View', '/posts/view/' . $id)
//posts/view/id

多次然后决定/posts应该在网址中调用/articles

//物品/视图/ ID

我不想更改现有代码,我想使用反向路由。

我读到了反向路由,但找不到任何合适的例子

与我的问题有关。

如果有人给我解决我的问题,我将不胜感激?

1 个答案:

答案 0 :(得分:0)

不要对网址进行硬编码,而是使用数组

$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]+'
   )
);
相关问题