CakePHP的宁静路线 [英] CakePHP restful routes
问题描述
我cannnot找到任何有关这个有用。我试图创建RESTful路由。以下是我希望我的路线是这样的:
I cannnot find anything useful about this. I am trying to create RESTful routing. Here is what I want my routes to look like:
`GET` routes to `RecipesController::api_index() //instead of RecipesController::index()`
`POST` routes to `RecipesController::api_add() //instead of RecipesController::add()`
`PUT` routes to `RecipesController::api_edit($id) //instead of RecipesController:edit($id)`
`DELETE` routes to `RecipesController::api_delete($id) //instead of RecipesController:delete($id)`
我不知道如何做到这一点,这里是我迄今为止:
I do not know how to do this, here is what I have so far:
Router::resourceMap(array(
array('prefix'=>'api', 'action' => 'index', 'method' => 'GET', 'id' => false),
array('prefix'=>'api', 'action' => 'view', 'method' => 'GET', 'id' => true),
array('prefix'=>'api', 'action' => 'add', 'method' => 'POST', 'id' => false),
array('prefix'=>'api', 'action' => 'edit', 'method' => 'PUT', 'id' => true),
array('prefix'=>'api', 'action' => 'delete', 'method' => 'DELETE', 'id' => true),
array('prefix'=>'api', 'action' => 'update', 'method' => 'POST', 'id' => true)
));
Router::mapResources('recipes', array('prefix'=>'api'));
在我的 core.php中
我有以下内容:
Configure::write('Routing.prefixes', array('api'));
蛋糕的回报是:
404失踪Recepies :: api_1()
404 missing Recepies::api_1()
下面是 CakePHP的文档我去了
此外,关于正确的RESTful API设计任何其他建议都大大AP preciated! :)
Also, any other suggestions about proper RESTful API design are greatly appreciated! :)
推荐答案
看起来你可以做到这一点。
Looks like you can do this
Router::mapResources('recipes', array('prefix'=>'api'));
和这将使你预期的URL。你也必须定义prefixes,像@johhniedoe指出。
and that will give you the expected urls. You have to define the prefixes also, like @johhniedoe pointed out.
这是API文档,我读从(它是2.0版,但由于工作V1.3),也许这会帮助你。最重要的部分是它说:
This is the api doc where I read that from (it's for v2.0, but works since v1.3), maybe it'll help. The important part is where it says
preFIX' - 网址preFIX要用于生成路线。默认为
/'.
'prefix' - URL prefix to use for the generated routes. Defaults to '/'.
有关的选项。所以,如果你激活preFIX并将其添加到 MA presources
,你就不需要做任何事情更多,有你想要的东西。如果你想路由是默认的,你不需要使用 resourcesMap
或者你定义了另一条路线, MA presources
应处理所有对自己。
for the options. So if you activate the prefix and add it to mapResources
, you wouldn't need to do anything more to have what you want. If the routes you want are the default, you don't need to use resourcesMap
or the other route you define, mapResources
should handle all that on its own.
编辑:
如果默认值需要重写
Router::resourceMap(array(
array('prefix'=>'api', 'action' => 'index', 'method' => 'GET', 'id' => false),
array('prefix'=>'api', 'action' => 'view', 'method' => 'GET', 'id' => true),
array('prefix'=>'api', 'action' => 'add', 'method' => 'POST', 'id' => false),
array('prefix'=>'api', 'action' => 'edit', 'method' => 'PUT', 'id' => true),
array('prefix'=>'api', 'action' => 'delete', 'method' => 'DELETE', 'id' => true),
array('prefix'=>'api', 'action' => 'update', 'method' => 'POST', 'id' => true)
));
Router::mapResources('recipes', array('prefix'=>'api'));
这篇关于CakePHP的宁静路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!