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?


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

// Redirect if set by the controller

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!

1 Answer 1


If you wanted to follow the same approach then I would say that you should create your own Dispatcher class, which extends the Joomla ComponentDispatcher, but overrides the dispatch() function.

If you define a Dispatcher class with your namespace then the ComponentDispatcherFactory will automatically find it, without needing to change your services/provider.php. It might sound a bit complicated but it's really easy to do. Add a comment if you want more detail about how to go about it.

I would also recommend the web asset manager if you're not already using it, as it makes your life so much easier.

  • Thanks! I'm going to explore both of those options further. I stubbed my toe on the WebAssetManager early on in my upgrade activities when one of my system plugins tried to inject a jQuery asset or something before the WAM had been instantiated, but I've not explored it in detail yet, though have read through that page now. I have to say, I am mystified as to how I have never encountered manual.joomla.org before, in all my years of Googling Joomla related APIs and dev activities. It does not appear to be linked to directly from the Joomla home page at least. Is that a recent introduction?
    – John Rix
    Commented Mar 13 at 10:20
  • Yes, it's fairly recent. And it's had lots of gaps which I've been trying recently to fill, so it's only recently got enough content to make it viable as a documentation source. By the way, I also did a video on the web asset manager at youtu.be/QCtfJLsArF4 which you might find useful. Commented Mar 13 at 10:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.