我使用的是CakePHP 2.4.6。
在routes.php中,我将
Router::mapResources('themes');
然后,正如我所期望的那样,我可以访问由控制器“主题”的操作“视图”生成的'localhost/themes/211‘显示屏幕。
视图文件中的BUt,我使用
<?php
echo $this->Html->url(array(
'controller' => 'themes',
'action' => 'view',
$theme['Theme']['id']));
?>
然后,这将在html中生成“/themes/view/211”,而我期望的是“/themes/211”。
$this->Html-> URL ()会生成RESTful URL吗?
如果没有,Cake view文件如何以其他方式生成RESTful URL?
我做错了什么吗?
提前谢谢。
发布于 2014-04-01 17:07:59
将操作保留为空
<?php
echo $this->Html->url(array(
'controller' => 'themes',
'action' => '',
$theme['Theme']['id']));
?>
或,
<?php
echo $this->Html->url(array(
'controller' => 'themes',
'action' =>$theme['Theme']['id']
));
?>
发布于 2014-04-01 17:47:29
如果您希望这样做,请转到您的\app\Config\routes.php并添加Router::connect('/themes', array('controller' => 'themes', 'action' => 'view'));
你不需要改变
<?php
echo $this->Html->url(array(
'controller' => 'themes',
'action' => 'view',
$theme['Theme']['id']));
?>
发布于 2014-04-01 17:53:31
您可能还需要定义更多Restfull api调用,因此可能需要定义一些更通用的调用:
Router::connect('/api/:controller', array('api' => true, 'action' => 'index', "[method]" => "GET"), array('pass' => array('id'), 'id' => '([0-9]+|me)'));
Router::connect('/api/:controller', array('api' => true, 'action' => 'add', "[method]" => "POST"), array('pass' => array('id'), 'id' => '([0-9]+|me)'));
Router::connect('/api/:controller/:id', array('api' => true, 'action' => 'view', "[method]" => "GET"), array('pass' => array('id'), 'id' => '([0-9]+|me)'));
Router::connect('/api/:controller/:id', array('api' => true, 'action' => 'edit', "[method]" => "POST"), array('pass' => array('id'), 'id' => '([0-9]+|me)'));
这些路由调用类似于"api/ Themes /123“到主题控制器中的操作索引,但是,如果您尝试执行POST请求,它将路由到add操作。
https://stackoverflow.com/questions/22780787
复制相似问题