2016-09-20 106 views
0

我曾在多次创建使用字符串蛋糕2.x的网址,像反向路由在CakePHP 2

$this->Html->link('View', '/posts/view/' . $id) 
//posts/view/id 

再后来决定/posts应在URL被称为/articles代替。

//篇/图/ ID

我不想改变我现有的代码,我想用反向路由。

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

有关我的问题。

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

回答

0

不要硬编码的网址,请使用数组来代替

$this->Html->link('View', array('controller' => 'posts', 'action' => 'view' ,$id)); 

而在你的路由文件(应用程序/配置/ 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]+' 
    ) 
);