Web services $this->;Html->;url()是否生成RESTful url?
我使用CakePHP2.4.6 在routes.php中,我将Web services $this->;Html->;url()是否生成RESTful url?,web-services,rest,cakephp,Web Services,Rest,Cakephp,我使用CakePHP2.4.6 在routes.php中,我将 Router::mapResources('themes'); 然后,我可以访问“localhost/themes/211”,它显示由控制器“themes”的操作“view”生成的屏幕,正如我所期望的那样 但在视图文件中,我使用 <?php echo $this->Html->url(array( 'controller' => 'themes', 'action' => 'v
Router::mapResources('themes');
然后,我可以访问“localhost/themes/211”,它显示由控制器“themes”的操作“view”生成的屏幕,正如我所期望的那样
但在视图文件中,我使用
<?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视图文件如何以其他方式生成RESTful URL
我做错什么了吗
提前感谢。将操作留空
<?php
echo $this->Html->url(array(
'controller' => 'themes',
'action' => '',
$theme['Theme']['id']));
?>
或者
如果希望发生这种情况,请转到\app\Config\routes.php并添加
Router::connect('/themes',数组('controller'=>'themes','action'=>'view')代码>
你不必改变
<?php
echo $this->Html->url(array(
'controller' => 'themes',
'action' => 'view',
$theme['Theme']['id']));
?>
您可能需要定义更多的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”)到Themes控制器中的操作索引,但是,如果您尝试执行POST请求,它将路由到add操作。但无论采用哪种方式,如果我选择返回而不使用REST,我也必须修改返回视图文件。因为在这种情况下,通过您的方式生成的url没有连接到任何操作/控制器。如果可能的话,我更喜欢在这两种情况下都有用的方式,使用REST和不使用REST。我理解在routes.php中放置“Router::mapResources('themes');”并不意味着自动启用Html->url()来生成RESTful url。是的,你是对的,你需要明确,因为通常/themes指的是/themes/index,而不是themes/view,所以您需要在routes.phpThanks,Joris!我理解在routes.php中放置“Router::mapResources('themes');”并不意味着自动启用Html->url()来生成RESTful url。如果我需要制作api,我将使用如果我需要制作api,我将使用您的解决方案。但在我的情况下,我不必制作api。我希望使用RESTfulURL的原因是,我认为在web应用程序中使用RESTfulURL和固定lilmited操作意味着在阅读Rails手册时可以更好地进行开发。
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)'));