CakePHP restful routes

2019-08-23 04:29发布

问题:

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'));

In my core.php I have the following:

Configure::write('Routing.prefixes', array('api'));

Cake returns this:

404 missing Recepies::api_1()

Here is the CakePHP documentation I go by

Also, any other suggestions about proper RESTful API design are greatly appreciated! :)

回答1:

Looks like you can do this

Router::mapResources('recipes', array('prefix'=>'api'));

and that will give you the expected urls. You have to define the prefixes also, like @johhniedoe pointed out.

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' - URL prefix to use for the generated routes. Defaults to '/'.

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.

EDIT:

If the defaults need override

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'));


回答2:

In Config/core.php:124 for me ( depends on your version ) there is definition of prefixes with them you can achieve what you want.

Configure::write('Routing.prefixes', array('api'));

You may also find this link helpful: http://book.cakephp.org/2.0/en/development/routing.html#using-additional-conditions-when-matching-routes

have a good one.