I see that some of you are still not convinced that the extension object is a cool concept.
Remember when I told you that the Joomla DIC — therefore the component's DIC which is a customised copy of it — is not a real Dependency Injection Container but a Service Locator and lamented the fact that you cannot get a reference to it from anywhere in your component, unlike modern PHP frameworks like Laravel?
Well, it turns out that you can get access to your component's DIC anywhere, anytime. You just need to add the following handy code to your extension's class:
protected static $dic; public function boot(ContainerInterface $container) { self::$dic = $container; } public static function getContainer() { if (empty(self::$dic)) { Factory::getApplication() ->bootComponent('com_example'); } return self::$dic; }
This code is very simple. We declare a protected static variable holding our component's DIC. This is initialised by the boot method which is called whenever the component is booted. The static method getContainer returns the component's DIC; if the DIC was undefined at this point it boots the component first.
So now you can simply do:
$exampleDIC = \Acme\Component\Example\Administrator\Extension\ExampleComponent::getContainer();
The component's DIC gives you access to all of the application's services and your component's service. Using the MVCFactory to get an MVC object you can be sure that it will work as intended.