0

我在 PHP 中使用 SLIM 做我的第一个项目。调用类时,我收到以下错误消息:

错误信息:

Fatal error: Uncaught RuntimeException: App\Controllers\CitasController:getAll is not resolvable in /var/www/html/slim/vendor/slim/slim/Slim/CallableResolver.php:170
Stack trace: #0 /var/www/html/slim/vendor/slim/slim/Slim/CallableResolver.php(109): Slim\CallableResolver->assertCallable()
#1 /var/www/html/slim/vendor/slim/slim/Slim/CallableResolver.php(72): Slim\CallableResolver->resolveByPredicate() 
#2 /var/www/html/slim/vendor/slim/slim/Slim/Routing/Route.php(366): Slim\CallableResolver->resolveRoute() 
#3 /var/www/html/slim/vendor/slim/slim/Slim/MiddlewareDispatcher.php(81): Slim\Routing\Route->handle() 
#4 /var/www/html/slim/vendor/slim/slim/Slim/MiddlewareDispatcher.php(81): Slim\MiddlewareDispatcher->handle() 
#5 /var/www/html/slim/vendor/slim/slim/Slim/Routing/Route.php(341): Slim\MiddlewareDispatcher->handle() 
#6 /var/www/html/slim/vendor/slim/slim/Slim/Routing/RouteRunner.php(84): Slim\Routing\Route->run() 
#7 /var/www/html/slim/vendor/slim/slim/Slim/MiddlewareDispatcher.php(81): Slim\Routing\RouteRunner->handle() 
#8 in /var/www/html/slim/vendor/slim/slim/Slim/CallableResolver.php on line 170

项目结构:

[ ]

  1. 作曲家.json
    {
        "require": {
            "slim/slim": "^4.9",
            "slim/psr7": "^1.5",
            "slim/php-view": "^3.1",
            "respect/validation": "^1.1",
            "php-di/php-di": "^6.0"
        },
        "autoload": {
            "psr-4": {
                "App\\": "src/"
            }
        }
    }
  1. 索引.php
    <?php
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);

    require __DIR__ . '/../src/App/App.php';
  1. 应用程序.php
    <?php
    use Slim\Factory\AppFactory;
    require __DIR__ . '/../../vendor/autoload.php';
    $app = AppFactory::create();
    require __DIR__ . "/Routes.php";
    $app->run();
  1. 路由.php
    <?php
    use Slim\Routing\RouteCollectorProxy;
    $app->group('/api',function(RouteCollectorProxy $group){
        $group->get('/citas','App\Controllers\CitasController:getAll');
    });

    $app->get('/', function (Request $request, Response $response, $args) {
        $response->getBody()->write("Hello world!");
        return $response;
    });
  1. CitasController.php
    <?php
    namespace App\Controllers;


    use Psr\Http\Message\ResponseInterface as Response;
    use Psr\Http\Message\ServerRequestInterface as Request;


    class CitasController{
        
        public function getAall($request, $response, $arg){
            $response->getBody()->write("Hollaaa");
            return $response;
        }
    }
4

1 回答 1

0

这里没有关于类加载问题的错误。您已经定义了一个带有无效可调用对象的路由:

/**
 * @param mixed $resolved
 * @param mixed $toResolve
 *
 * @throws RuntimeException
 *
 * @return callable
 */
private function assertCallable($resolved, $toResolve): callable
{
    if (!is_callable($resolved)) {
        throw new RuntimeException(sprintf(
            '%s is not resolvable',
            is_callable($toResolve) || is_object($toResolve) || is_array($toResolve) ?
                json_encode($toResolve) : $toResolve
        ));
    }
    return $resolved;
}

错误投诉CitasController:getAll()但您有public function getAall($request, $response, $arg).

于 2021-12-17T17:37:18.770 回答