com_mycomponent/services/provider.php
:
return new class implements ServiceProviderInterface {
public function register(Container $container): void {
$container->registerServiceProvider(new MVCFactory('\\MyCompany\\Component\\MyComponent'));
$container->registerServiceProvider(new ComponentDispatcherFactory('\\MyCompany\\Component\\MyComponent'));
$container->set(
ComponentInterface::class,
function (Container $container) {
$component = new MyComponent($container->get(ComponentDispatcherFactoryInterface::class));
$component->setMVCFactory($container->get(MVCFactoryInterface::class));
$container->set(
"ANumber",
fn() => 1,
);
return $component;
}
);
}
};
com_mycomponent/src/Controller/MyController.php
:
class MyController extends BaseController {
private int $myNumber;
public function __construct($config = [], MVCFactoryInterface $factory = null, ?CMSApplication $app = null, ?Input $input = null) {
parent::__construct($config, $factory, $app, $input);
$this->myNumber = $container->get("ANumber");
}
public function display($cachable = false, $urlparams = []) {
// do something with $this->myNumber
return parent::display($cachable, $urlparams);
}
}
I am migrating to Joomla 4/5 and have a question after reading the documentation. Is it possible to access the dependencies that I registered in my provider.php
file from a controller?