slim3-controller
slim3-controller copied to clipboard
$this->get('/','\Controller\adminController:index');
i think this route more better.$this->get('/','\Controller\adminController:index');
but error. Catchable fatal error: Argument 1 passed to MartynBiz\Slim3Controller\Controller::__construct() must be an instance of Slim\App, instance of Slim\Container given, called in D:\phpStudy\WWW\slimdemo\vendor\slim\slim\Slim\CallableResolver.php on line 64 and defined in D:\phpStudy\WWW\slimdemo\vendor\martynbiz\slim3-controller\src\Controller.php on line 16
how assgin app instance to MartynBiz\Slim3Controller\Controller::__construct()
Hi. You can see an example in the readme:
// index routes (homepage, about, etc)
$app->group('', function () use ($app) {
$controller = new App\Controller\IndexController($app);
$app->get('/', $controller('index'));
$app->get('/contact', $controller('contact'));
});
Slim\App is passed in during the construct of the controller. When the controller object is invoked - e.g. $controller('contact') - all it is doing is generating a Closure. When that Closure is called (if the route is matched) it will call the desired controller action method.
You suggestion is neat, however with my implementation I use the $app object when building the Closure and I want the $app object to be available to the action methods (e.g. so I can access container) - so I pass it into the controller that way. Feel free to fork it anyway if you have something else in mind, I'd be interested to see how else it might be done.