Welcome Guest, Not a member yet? Register   Sign In
A way to override the instantiation of the controller
#1

I'd like to stop referencing the dependency injection container (DIC). I'm force to do this, because the controller is created with the new keyword. If I could override this behavior and have the DIC create the controller, I can get the dependencies I need by adding them to the constructor.

For example
PHP Code:
//what I currently have to do
class currentController extends controller
{
    public function 
index()
    {
        
Container::staticMethodToGetContainerInstance()
            ->
createInstance(DependencyIWant::class)
            ->
doSomeWork();
    }
}

//what I want to do
class theControllerIWant extends controller
{
    private 
$dependency;

    public function 
__construct(DependencyIWant $dependency)
    {
        
$this->dependency $dependency;
    }

    public function 
index()
    {
        
$this->dependency
            
->doSomeWork();
    }


I do have a branch https://github.com/eavMarshall/CodeIgniter4 that should allow me to do this, by adding a common function that can be overwritten
Reply


Messages In This Thread
A way to override the instantiation of the controller - by eavmarshall - 02-27-2021, 11:14 AM



Theme © iAndrew 2016 - Forum software by © MyBB