doubo1883 2017-04-17 10:15
浏览 63
已采纳

Slim Framework:将HTTP请求路由到静态类方法

I just started using Slim Framework to create my rest API. Everything works well until I try to route HTTP request to a static class method (I used the anonymous function before). Below is my new route code on index.php:

include "vendor/autoload.php";
$config = ['settings' => [
               'addContentLengthHeader' => false,
               'displayErrorDetails'    => true,
               'determineRouteBeforeAppMiddleware' => true
            ]
          ];

$app = new \Slim\App($config);
$app->get('/user/test', '\App\Controllers\UserController:test');
$app->run();

And below is my UserController class on UserController.php

class UserController{
    public function test($request, $response, $args){
        $array = ['message'=>'your route works well'];
        return $response->withStatus(STAT_SUCCESS)
                        ->withJson($array);
    }
}

Error details:

Type   : RuntimeException
Message: Callable \Controllers\UserController does not exist
File   : /var/www/html/project_api/vendor/slim/slim/Slim/CallableResolver.php

Below is my project folder tree

project_api/
           index.php
           vendor/
                 slim/slim/Slim/CallableResolver.php

           Controllers/
                      UserController.php

my composer.json

{
    "require": {
        "slim/slim": "^3.8",
        "sergeytsalkov/meekrodb": "*",
        "slim/http-cache": "^0.3.0"
    }
},
"autoload": {
    "psr-4": {
        "Controllers\\": "Controllers/"
    }
}
  • 写回答

1条回答

  • duanaiguang1960 2017-04-18 03:20
    关注

    It seems that your namespace is define improperly. In your composer.json, class UserController under the namespace Controllers.

    you should define a namespace at the top of your UserController.php:

    namespace Controllers;
    

    and change $app->get() in your index.php to:

    $app->get('/user/test', 'Controllers\UserController:test');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧