0

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
tanmay_garg
  • 377
  • 1
  • 13
jelly5798
  • 349
  • 2
  • 9
  • 2
    I don't know Slim 4, but I'll assume it's because you're not including the `\App` namespace in the second one. Try doing `$app->get('/test2', '\App\Test2Controller:home');`? – chaseisabelle May 23 '20 at 10:45
  • Either, as suggested by previous comment, use `\App\Test2Controller:home` as the route callback, or like what you did with `/test1`, do the same and use `Test2Controller::class.':home'` – Nima May 23 '20 at 11:05
  • Oh I'm such an idiot! To use an expression I "can't see the wood for the trees". Thanks – jelly5798 May 23 '20 at 11:29

0 Answers0