处理 API 项目。一切都很好,直到我必须添加新端点(也是新方法)并且我的新端点出现此错误,另一个端点运行良好。以下是错误日志:
Type: RuntimeException
Message: [{“container”:{}},“getNotification”] is not resolvable
File: /opt/myanime/api/vendor/slim/slim/Slim/CallableResolver.php
Line: 104
Trace
#0 /opt/myanime/api/vendor/slim/slim/Slim/CallableResolver.php(62): SlimCallableResolver->assertCallable(Array)
#1 /opt/myanime/api/vendor/slim/slim/Slim/CallableResolverAwareTrait.php(45): SlimCallableResolver->resolve(‘UserController:…’)
#2 /opt/myanime/api/vendor/slim/slim/Slim/Route.php(333): SlimRoutable->resolveCallable(‘UserController:…’)
#3 /opt/myanime/api/vendor/slim/slim/Slim/MiddlewareAwareTrait.php(122): SlimRoute->__invoke(Object(SlimHttpRequest), Object(SlimHttpResponse))
#4 /opt/myanime/api/vendor/slim/slim/Slim/Route.php(316): SlimRoute->callMiddlewareStack(Object(SlimHttpRequest), Object(SlimHttpResponse))
#5 /opt/myanime/api/vendor/slim/slim/Slim/App.php(476): SlimRoute->run(Object(SlimHttpRequest), Object(SlimHttpResponse))
#6 /opt/myanime/api/src/MyanimeMiddleware.php(121): SlimApp->__invoke(Object(SlimHttpRequest), Object(SlimHttpResponse))
#7 [internal function]: AppMyanimeMiddleware->__invoke(Object(SlimHttpRequest), Object(SlimHttpResponse), Object(SlimApp))
#8 /opt/myanime/api/vendor/slim/slim/Slim/DeferredCallable.php(43): call_user_func_array(Object(AppVestiaMiddleware), Array)
#9 [internal function]: SlimDeferredCallable->__invoke(Object(SlimHttpRequest), Object(SlimHttpResponse), Object(SlimApp))
#10 /opt/myanime/api/vendor/slim/slim/Slim/MiddlewareAwareTrait.php(73): call_user_func(Object(SlimDeferredCallable), Object(SlimHttpRequest), Object(SlimHttpResponse), Object(SlimApp))
#11 /opt/myanime/api/vendor/slim/slim/Slim/MiddlewareAwareTrait.php(122): SlimApp->Slim{closure}(Object(SlimHttpRequest), Object(SlimHttpResponse))
#12 /opt/myanime/api/vendor/slim/slim/Slim/App.php(370): SlimApp->callMiddlewareStack(Object(SlimHttpRequest), Object(SlimHttpResponse))
#13 /opt/myanime/api/vendor/slim/slim/Slim/App.php(295): SlimApp->process(Object(SlimHttpRequest), Object(SlimHttpResponse))
#14 /opt/myanime/api/public/index.php(35): SlimApp->run()
#15 {main}
我的新终结点的路由:
$app->get('/user/notification', 'UserController:getNotification');
我的班级:
class UserController{
public function __construct($container)
{
// make the container available in the class
$this->container = $container;
}
public function getNotification($request, $response, $args){
//my code here
}
}
我的dependencies.php
:
use AppControllersUserController;
$container = $app->getContainer();
$container['UserController'] = function ($c) {
return new UserController($c);
};
我的composer.json
{
"name": "slim/slim-skeleton",
"autoload": {
"psr-4": {
"App\": "src/"
}
}
}
试图composer dump-autoload
但没有奏效。
PHP 找不到你的类。
- 在索引中需要它.php
- 修复 PSR-0/4 自动加载架构,然后转储自动加载
rsync
服务器时不小心。我上传到错误的目录Controller
而不是Controllers
.
我遇到了类似的问题,我到了那里。就我而言,Slim 无法实例化控制器类,因为它只是拼写错误。
根据提供的代码,'UserController:getNotification'
不是有效的可调用对象。使用静态函数'UserController::getNotification'
或构造用户控制器并将array($controller, "getNotification")
传递给$app->get()
。