I am using the Slim 4 framework and trying to get the routing working, but for the life of me I cannot figure out why my controller file
cannot be found. I'm used to laravel
taking care of all these things for me, but I wanted to try slim
for my latest application.
I have been using http://www.slimframework.com/docs/v4/objects/routing.html as my guide and want my routes to be defined like ControllerHere:FunctionHere
, but for some reason it does not work.
From my index.php
I include a routes.php
file, the contents of which is
namespace App;
$app->get('/test1', Test1Controller::class.':home');
$app->get('/test2', '\Test2Controller:home');
I then have two files: Test1Controller
and Test2Controller
, both in the same app folder as my routes.php
file.
Following the guide in the link my /test1
route and Test1Controller load perfectly, but I cannot get Test2Controller to work, the code for which is:
namespace App;
use Psr\Container\ContainerInterface;
class Test2Controller {
protected $container;
public function __construct(ContainerInterface $container) {
$this->container = $container;
}
public function home($request, $response, $args) {
$response->getBody()->write("Hello world test2!");
return $response;
}
}
Can someone see what I'm doing wrong? The error I get is:
PHP Fatal error: Uncaught RuntimeException: Callable \\Test2Controller does not exist