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

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号