donglin8467
2016-05-13 08:21
浏览 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');
    });

图片转代码服务由CSDN问答提供 功能建议

我正在使用 CakePHP 3.1.13构建 RESTful API系统 / strong>(我不能使用 3.2.x ,因为服务器PHP版本是5.5.x )。

我的控制器名称是 CmsCouplesController.php 和url: http://localhost/~emanuele/works/grai/html/api/v1/cms-couples.json \ n

正常工作。

但是另一个电话( http://localhost/~emanuele/works/grai/html/api/v1/cms-couples/1.json )返回:

无法找到或无法访问操作CmsCouplesController :: 1()。

如果我创建一个控制器 CouplesController.php 一切正常。

为什么?!

UPDATE:路由配置

  Router :: scope('  /',函数($ routes){
 
 
 $ routes->前缀('v1',函数($ 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

    点赞 评论
  • dqyin0101 2016-05-13 10:01

    from my understanding...

    http://localhost/~emanuele/works/grai/html/api/v1/cms-couples.json
    

    must be pointing to index function of CmsCouplesController.php controller

    then for what reason you want this kind of url

     http://localhost/~emanuele/works/grai/html/api/v1/cms-couples/1.json
    

    you can give url like

    http://localhost/~emanuele/works/grai/html/api/v1/cms-couples/json-request
    

    then you can put your code in jsonReuest function of the CmsCouplesController.php controller ...

    If this does not help then explain your question with answer of my question of why you want URl like 1.json

    点赞 评论

相关推荐 更多相似问题