I am trying to get my slim application to work, but I get this error:
( ! ) Fatal error: Uncaught RuntimeException: Callable Quiz\Controller\QuizController::index() does not exist in /var/www/html/vendor/slim/slim/Slim/CallableResolver.php on line 138
( ! ) RuntimeException: Callable Quiz\Controller\QuizController::index() does not exist in /var/www/html/vendor/slim/slim/Slim/CallableResolver.php on line 138
I'm using PHP 8.1 and Slim 4.11
My autoload config:
"autoload": {
"psr-4": {"Quiz\\": "source/"}
}
Method which should be called:
<?php
declare(strict_types=1);
namespace Quiz\Trivia\Controller;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Slim\Http\Response;
final class QuizController
{
private $container;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
public function index(ServerRequestInterface $request, Response $response, array $args): ResponseInterface
{
return $this->container->get('views')->render(
$response,
'index.twig'
);
}
}
I define the route in routes.php as follows:
<?php
declare(strict_types=1);
use Quiz\Trivia\Controller\QuizController;
return [
'index' => [
'type' => 'get',
'path' => '/',
'class' => QuizController::class . ':index'
]
];
Later, I add the route in index.php with $app->map(...) to the slim app.
I already tried some solutions from older posts, but nothing worked so far. So I think I'm missing something crucial.
Thanks in Advance!
This is because your autoload config is set to "Quiz\": "source/
so it expects to find all classes belonging to the Quiz
namespace in that folder, when in fact the controller you're trying to use is not located there.
You should move the trivia
folder inside your source directory (with a capital T
to respect conventions) or update the map to include the trivia namespacing and it's directory.
"Quiz\": "source/
"Quiz\\Trivia\": "trivia/"
(the double-backslash \\
is intentional.)