【问题标题】:CakePHP restful routesCakePHP 宁静的路线
【发布时间】:2016-02-17 02:14:14
【问题描述】:

我找不到任何有用的东西。我正在尝试创建 RESTful 路由。这是我希望我的路线的样子:

`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)`

我不知道该怎么做,这是我目前所拥有的:

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

Cake 返回这个:

404 缺少 Recepies::api_1()

这是我参考的CakePHP 文档

此外,非常感谢任何其他有关正确 RESTful API 设计的建议! :)

【问题讨论】:

    标签: php api rest cakephp cakephp-2.0


    【解决方案1】:

    看起来你可以做到这一点

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

    这将为您提供预期的网址。您还必须定义前缀,就像@johhniedoe 指出的那样。

    This 是我从中读取的 api 文档(它适用于 v2.0,但从 v1.3 开始工作),也许它会有所帮助。重要的部分是它说的地方

    'prefix' - 用于生成路由的 URL 前缀。默认为 '/'。

    对于选项。因此,如果您激活前缀并将其添加到mapResources,您无需再做任何事情即可获得您想要的。如果您想要的路由是默认路由,则不需要使用resourcesMap 或您定义的其他路由,mapResources 应该自己处理所有这些。

    编辑:

    如果需要覆盖默认值

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

    【讨论】:

    • 感谢您的回答。这对我不起作用:(它适用于 GET 但是当我删除 /api/recepies/1 时它返回 404 缺少 Recepies::api_1()。我希望它转到 REcepies::api_delete(1) 任何想法如何修复它?我知道 DELETE /api/recepies/delete/1 会起作用,但我试图远离它。
    • 我假设“receipies”是一个错字,对吧?检查所有其他方法,如果它适用于 GET 和所有其他方法,但 DELETE 可能是调用 REST 操作的函数的问题。如果该部分没有错误,请尝试使用 resourceMap 覆盖默认值(我将为此更新答案)
    • 它对任何一个都不起作用,我只是以 DELETE 为例。谢谢,我会尝试,如果有效,我会将答案标记为已接受。感谢您就此事回复我。
    • 我得到了和上次一样的结果——返回 404 缺少 Recepies::api_1()。这应该很简单,我不知道为什么它根本没有按预期工作。我真的没有任何其他路线可能会搞砸这件事。
    • 我现在在黑暗中调试...但是让我们看看。我认为 cake 没有正确识别调用方法,因此没有将 recipes/1 之类的路由关联为 REST。你能用你用来测试的代码更新你的问题吗?另外,为了好玩,请尝试使用recipes/1.html(或您想要的格式并且已经制作了视图)
    【解决方案2】:

    在我的 Config/core.php:124 中(取决于你的版本)有前缀的定义,你可以实现你想要的。

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

    此链接可能对您也有帮助:http://book.cakephp.org/2.0/en/development/routing.html#using-additional-conditions-when-matching-routes

    祝你好运。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-26
      • 1970-01-01
      • 1970-01-01
      • 2011-04-30
      • 1970-01-01
      • 2013-05-26
      相关资源
      最近更新 更多