For a custom component in Joomla 4 (and 5), is there a way to have some component code that gets executed after the controller execution step, regardless of which controller is triggered?
Why?
In Joomla 1.5, 2 and 3, I had some code in my component that would automatically locate and inject supporting JS and CSS files that matched the view and layout of the page being rendered. This was a single function call into a library function for the purpose, and was invoked as illustrated below, from within the main component PHP file.
Part of the reason for having this particular step after the $controller->execute(...)
step is that often the controller will set or adjust the view and layout values eg. if these are not explicitly set in the request.
// Create the controller
$controller = new $controllerName();
// Perform the requested task
$controller->execute( $task );
// Include view and layout specific JS and CSS files if they exist
MyComponentHelper::includeViewFiles();
// Redirect if set by the controller
$controller->redirect();
In Joomla 4, now with DI, it is not obvious how I might achieve the same result. Should I be somehow creating my own custom ComponentDispatcher or ComponentDispatcherFactory class to supply to the DI container? (Probably not.)
Alternatively, if there is a more correct way for me to inject my JS and CSS files (without needing to update every view or every individual controller - I have a lot!), I am also keen to know about that!