route: Could not instance Controller with container.
Please provide login for instantiating Controllers with DI Container, because right now I have to write my own strategy and implement a logic for $route->getCallable() which make call inside from file Route.php line 87
My code flow:
class HomeController extends BaseController
{
public function home($request, $response) {
return $response;
}
}
abstract class BaseController
{
use \League\Container\ContainerAwareTrait;
}
`
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Comments: 17 (8 by maintainers)
I don’t have much time right now so haven’t read the whole thread yet. Just want to touch on your question above though.
Passing the container in to your controllers and pulling dependencies from the container is actually Service Location, not Dependency Injection and is considered an anti pattern for many reasons.
It is something you can do but I don’t recommend that you do. A better approach is to have your controllers set up to receive their dependencies directly via the constructor. If you do that, you can then define those dependencies in the container and pass that container to the router, once you do that, the container will handle wiring everything together.