A way to override the instantiation of the controller |
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 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
Interesting. Which branch is it?
(02-27-2021, 10:34 PM)kenjis Wrote: Interesting. Which branch is it? Sorry I think I closed my laptop before it pushed, I've made sure the changes are pushed this time. I've made the changes on developed on my fork. https://github.com/codeigniter4/CodeIgni...ll:develop I'm not too sure which is the correct way to allow developers to override functionality. So I just added a function in the common.php file Allowing the developers to override the instantiation of the controller, in my case I would like the DI Container to create the controller and inject dependencies into that controller.
CodeIgniter uses Services in lieu of a Container implementation. Without looking into the code I suspect that a Controller Service could be implemented, but it would probably need to be a little different than your approach.
(02-27-2021, 11:34 PM)eavmarshall Wrote: I'm not too sure which is the correct way to allow developers to override functionality. So I just added a function in the common.php file This is a great idea! Inspired it, I will create a PR. I sent PR: https://github.com/codeigniter4/CodeIgniter4/pull/4370
https://github.com/codeigniter4/CodeIgniter4/pull/4376 was merged.
Now you can extend the class `CodeIgniter` like other core classes, and can override `CodeIgniter::createController()`. https://codeigniter4.github.io/CodeIgnit...asses.html
PHP Code: <?php PHP Code: <?php PHP Code: <?php PHP Code: <?php Worked perfectly, thanks
It is not perfect. Feature Testing won't work at all.
We also need to override MockCodeIgniter, but it was not simple. https://github.com/kenjis/ci4-tettei-app...d741c8aa36 |
Welcome Guest, Not a member yet? Register Sign In |