PHP-DI / Slim-Bridge

PHP-DI integration with the Slim framework
http://php-di.org/doc/frameworks/slim.html
MIT License
176 stars 38 forks source link

Error when I define a controller action on a route using class:method syntax #55

Closed ghost closed 4 years ago

ghost commented 4 years ago

When I define a controller action on a route using the class:method syntax in an application using Slim-Bridge, I get this error:

Slim Application Error
The application could not run because of the following error:

Details
Type: Invoker\Exception\NotCallableException
Code: 0
Message: 'App\\Controllers\\HomeController:index' is neither a callable nor a valid container entry
File: C:\Users\Ashura\Desktop\Projetos\spark\vendor\php-di\invoker\src\Exception\NotCallableException.php
Line: 33

I am using the following code:

use DI\Bridge\Slim\Bridge;

$app = Bridge::create();

$app->addRoutingMiddleware();
$app->addErrorMiddleware(true, true, true);

$app->get('/', '\App\Controllers\HomeController:index');

$app->run();

When I remove Slim-Bridge and add App Factory everything works:

$app = \Slim\Factory\AppFactory::create();

$app->addRoutingMiddleware();
$app->addErrorMiddleware(true, true, true);

$app->get('/', '\App\Controllers\HomeController:index');
mnapoli commented 4 years ago

Hi, indeed this syntax is not supported in the PHP-DI Bridge.

Have a look at the documentation to see how to define a controller as a service: http://php-di.org/doc/frameworks/slim.html#controllers-as-services