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

报告相同问题?