我剛開始使用Slim Framework創建我的rest API。一切正常,直到我嘗試將HTTP請求路由到靜態類方法(我之前使用匿名函數)。下面是我的新航線代碼上index.php
:Slim Framework:將HTTP請求路由到靜態類方法
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();
,下面將我的UserController
類上UserController.php
class UserController{
public function test($request, $response, $args){
$array = ['message'=>'your route works well'];
return $response->withStatus(STAT_SUCCESS)
->withJson($array);
}
}
錯誤的詳細信息:
Type : RuntimeException
Message: Callable \Controllers\UserController does not exist
File : /var/www/html/project_api/vendor/slim/slim/Slim/CallableResolver.php
下面是我的項目文件夾樹
project_api/
index.php
vendor/
slim/slim/Slim/CallableResolver.php
Controllers/
UserController.php
我composer.json
{
"require": {
"slim/slim": "^3.8",
"sergeytsalkov/meekrodb": "*",
"slim/http-cache": "^0.3.0"
}
},
"autoload": {
"psr-4": {
"Controllers\\": "Controllers/"
}
}
你調用的類'User'不'UserController' – jmattheis
喜@jmattheis,感謝您的糾正。更改了類名並仍然有相同的錯誤 – red
您是否已經定義了composer autoload? – jmattheis