CakePHP中的不同视图路由
我可以在CakePHP中的不同视图路由,php,cakephp,routing,Php,Cakephp,Routing,我可以在routes.php中更改简单URL的生成和接受路由: Router::connect('/login', array('controller' => 'users', 'action' => 'login')); Router::connect('/logout', array('controller' => 'users', 'action' => 'logout')); Router::connect('/register', array('controll
routes.php
中更改简单URL的生成和接受路由:
Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
Router::connect('/logout', array('controller' => 'users', 'action' => 'logout'));
Router::connect('/register', array('controller' => 'users', 'action' => 'add'));
这很有魅力。但是,这并不是:
Router::connect('/eintrag/:id', array('controller' => 'entries', 'action' => 'view'));
Router::connect('/bearbeiten/:id', array('controller' => 'entries', 'action' => 'edit'));
当我试图通过echo$this->Html->url(数组('controller'=>'entries'、'action'=>'view'、$entry['id'])
)获取此路径时,我得到了/entries/view/1
。路由器不接受url/eintrag/1
如何像处理无参数路由一样美化视图和编辑路由?$this->Html->url()只是一个帮助函数,它只根据传递的参数生成url,但是,当您实际打开此Url时,它会将/eintrag/1的请求路由到/entries/view/1您需要在路由中使用第三个参数,因为您正在传递它
:id
// SomeController.php
public function view($id = null) {
// some code here...
}
// routes.php
Router::connect(
'/eintrag/:id', // e.g. /eintrag/1
array('controller' => 'entries', 'action' => 'view'),
array(
// this will map ":id" to $id in your action
'pass' => array('id'),
'id' => '[0-9]+'
)
);
我应该这样做
更多信息@那么,当我有无参数路线时,为什么它可以这样做?