php 未捕获的运行时异常:可调用项不存在

k2fxgqgv  于 2023-02-15  发布在  PHP
关注(0)|答案(1)|浏览(104)

我正在尝试一个简单的超薄应用程序,如下所示。
index.php

<?php
use Slim\Factory\AppFactory;

require './vendor/autoload.php';

$app = AppFactory::create();

$app->get('/', [TestController::class, 'showBlank']);

$app->run();

下面是TestController.php

<?php
use \Psr\Container\ContainerInterface;
use Psr\Http\Message\ServerRequestInterface;
use Slim\Http\Interfaces\ResponseInterface;

class TestController
{
    protected $c;

    public function __construct(ContainerInterface $c)
    {
        $this->c = $c;
    }

    public function showBlank(ServerRequestInterface $request, ResponseInterface $response, array $args): ResponseInterface
    {
        return $response;
    }

}

以下是项目结构。

C:\laragon\www\chum>ls
composer.json  composer.lock  index.php  TestController.php  vendor

因为我是第一次尝试slim,所以我把这个例子和composer.json文件保持在最小限度。

{
  "require": {
    "slim/slim": "^4.11",
    "slim/psr7": "^1.6",
    "slim/twig-view": "^3.3",
    "slim/http": "^1.3"
  }
}

考虑到这些,当我试图访问根页面(\)时,我得到了下面的异常。

Fatal error: Uncaught RuntimeException: Callable TestController::showBlank() does not exist in C:\laragon\www\chum\vendor\slim\slim\Slim\CallableResolver.php:138 

Stack trace: 
#0 C:\laragon\www\chum\vendor\slim\slim\Slim\CallableResolver.php(90): Slim\CallableResolver->resolveSlimNotation('TestController:...') 
#1 C:\laragon\www\chum\vendor\slim\slim\Slim\CallableResolver.php(63): Slim\CallableResolver->resolveByPredicate('TestController:...', Array, 'handle') 
#2 C:\laragon\www\chum\vendor\slim\slim\Slim\Routing\Route.php(340): Slim\CallableResolver->resolveRoute(Array) 
#3 C:\laragon\www\chum\vendor\slim\slim\Slim\MiddlewareDispatcher.php(65): Slim\Routing\Route->handle(Object(Slim\Http\ServerRequest)) 
#4 C:\laragon\www\chum\vendor\slim\slim\Slim\MiddlewareDispatcher.php(65): Slim\MiddlewareDispatcher->handle(Object(Slim\Http\ServerRequest)) 
#5 C:\laragon\www\chum\vendor\slim\slim\Slim\Routing\Route.php(315): Slim\MiddlewareDispatcher->handle(Object(Slim\Http\ServerRequest)) 
#6 C:\laragon\www\chum\vendor\slim\slim\Slim\Routing\RouteRunner.php(68): Slim\Routing\Route->run(Object(Slim\Http\ServerRequest)) 
#7 C:\laragon\www\chum\vendor\slim\slim\Slim\MiddlewareDispatcher.php(65): Slim\Routing\RouteRunner->handle(Object(Slim\Http\ServerRequest)) 
#8 C:\laragon\www\chum\vendor\slim\slim\Slim\App.php(199): Slim\MiddlewareDispatcher->handle(Object(Slim\Http\ServerRequest)) 
#9 C:\laragon\www\chum\vendor\slim\slim\Slim\App.php(183): Slim\App->handle(Object(Slim\Http\ServerRequest)) 
#10 C:\laragon\www\chum\index.php(56): Slim\App->run() #11 {main} thrown in C:\laragon\www\chum\vendor\slim\slim\Slim\CallableResolver.php on line 138
eqqqjvef

eqqqjvef1#

感谢Alvaro在评论中的伟大提示,我能够解决它与一些进一步的帮助,从谷歌的基础上。
我假设扁平的项目结构不需要配置名称空间,但是我错了。
我对composer.json文件进行了以下更新,并运行了composer dump-autoload -o

"autoload": {
    "psr-4": {
      "App\\": ""
    }
  },

相关问题