donglin8467 2016-05-13 08:21 采纳率: 0%
浏览 24
已采纳

Cakephp 3.x:虚线休息api无效

I'm building a RESTful API System with CakePHP 3.1.13 ( i can't use 3.2.x because the Server PHP Version is 5.5.x ).

My controller name is CmsCouplesController.php and the url : http://localhost/~emanuele/works/grai/html/api/v1/cms-couples.json

works correctly.

BUT the other call ( http://localhost/~emanuele/works/grai/html/api/v1/cms-couples/1.json ) return :

Action CmsCouplesController::1() could not be found, or is not accessible.

If i create a controller CouplesController.php all works fine.

So why?!

UPDATE : routes configuration

Router::scope('/', function ($routes) {


    $routes->prefix('v1',function($routes) {
        $routes->extensions(['json','xml']);
        $routes->resources('Couples');
        $routes->fallbacks('DashedRoute');
    });
  • 写回答

2条回答 默认 最新

  • duanjiangzhi6851 2016-05-13 13:29
    关注

    Resource routes require separate inflection configuration

    You are missing the proper inflection configuration for your resource routes. By default resource routes are using underscore inflection, ie currently your resource routes will match cms_couples.

    Note that you can easily check which/how routes are connected by using the routes shell

    bin/cake routes
    

    It will show you something like

    | v1:cmscouples:index  | /v1/cms_couples     | {"controller":"CmsCouples","action":"index","_method":"GET","prefix":"v1","plugin":null}          |
    | v1:cmscouples:add    | /v1/cms_couples     | {"controller":"CmsCouples","action":"add","_method":"POST","prefix":"v1","plugin":null}           |
    | v1:cmscouples:view   | /v1/cms_couples/:id | {"controller":"CmsCouples","action":"view","_method":"GET","prefix":"v1","plugin":null}           |
    | v1:cmscouples:edit   | /v1/cms_couples/:id | {"controller":"CmsCouples","action":"edit","_method":["PUT","PATCH"],"prefix":"v1","plugin":null} |
    | v1:cmscouples:delete | /v1/cms_couples/:id | {"controller":"CmsCouples","action":"delete","_method":"DELETE","prefix":"v1","plugin":null}      |
    

    Long story short, use dasherize inflection and you should be good.

    $routes->resources('CmsCouples', [
        'inflect' => 'dasherize'
    ]);
    

    See also

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 找驱动QTR2774-SU-IBM版本驱动
  • ¥15 NeRF的代码复现以及人工智能入门
  • ¥15 centos如何做一个可以让ikuai连接的openvpn
  • ¥15 Protege 中的Entities下面的界面删除了如何复原
  • ¥15 scrapy爬虫求帮
  • ¥15 imageEnView绘图问题
  • ¥15 关于#python#的问题:您好可以加您一下联系方式吗,在复现的时候确实有点问题难以解决
  • ¥15 联想电脑重装系统时无法发现硬盘
  • ¥15 MATLAB与UR10e实体机械臂建立通讯
  • ¥15 c++题需要快一点不用opencv