-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
[5.1] Implement onAfterInitialiseRouter event #42692
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[5.1] Implement onAfterInitialiseRouter event #42692
Conversation
Co-authored-by: Brian Teeman <brian@teeman.net>
|
Looks good. Only a couple notes. joomla-cms/libraries/src/Service/Provider/Router.php Lines 42 to 50 in e2170c8
kind of: $router = new SiteRouter($container->get(SiteApplication::class));
$container->get(DispatcherInterface::class)->dispatch(
'onAfterInitialiseRouter',
new AfterInitialiseRouterEvent('onAfterInitialiseRouter', ['router' => $router])
);
return $router;This way it will be more modular, and consistent when at some point we decide to extend it to another Routers. Another note. As @wilsonge already wrote. |
|
@Hackwar please move the event dispatching out from the constructor, keep the constructor "light", then it is good to go 😉 It may looks a simple solution to place it there, but it will give a lot of headache to refactor this code later. |
|
@Fedik While I disagree with the change, it's not the hill I'm going to die on. As requested, this has been implemented. |
|
Visiting a home page, got an error:
|
|
I have tested this item ✅ successfully on 222e9ae This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/42692. |
|
I see, but you already set the router at
So you probably can revert |
|
I'm not happy that we have the get/setSiteRouter at all and I don't like the order in which this is set. I would either keep it the way it is with the getApplication()->getRouter() or get it directly from the DI container. But someone decided that we should deprecate the getRouter() and at the same time it is frowned upon to get this from the container directly. But the get/setSiteRouter also is nasty. Why do we have this for one specific application router? Why shouldn't we have this for all applications? And with this being called when the plugin is instantiated, the router is created before the system plugins are loaded, thus making it impossible to run a system plugin on the onAfterRouterInitialise event. It basically is just a matter of time when a third party extension uses this code and thus breaks this feature. |
|
You should not need an application to get the router from it. As only the SiteRouter is needed to create different urls, I decided to create that one first, if over time we need some other ones for more applications, then feel free to create new services. |
|
I have tested this item ✅ successfully on 222e9ae This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/42692. |
|
RTC This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/42692. |
|
I have tested this item 🔴 unsuccessfully on 07c4e6a This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/42692. |
|
Has been fixed. |
|
I would agree with you, however the global configuration code doesn't allow that. I'm going to revert the whole notice part and this apparently will have to go into an additional PR. |
|
I have tested this item ✅ successfully on eafa7fa This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/42692. |
|
I have tested this item ✅ successfully on eafa7fa This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/42692. |
|
RTC This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/42692. |
|
Thank you @Hackwar and also for testing and review @Fedik @laoneo @SniperSister @wilsonge @brianteeman |

Summary of Changes
We allow to modify the router by adding rules to it. So far often enough this was done by using a plugin and adding those rules on onAfterInitialise. This however poses a problem when we are not in the SiteApplication and want to create a link, since the router might not be instantiated properly. Often enough plugins are checking if the current application is the SiteApplication, and if not, they don't react. However that means when you are in the backend and create a link to the frontend for for example a mailing, the URL might be wrong, because the plugins with the custom behavior didn't attach that behavior to the router, since it thought it was in the wrong context.
This PR introduces the new
onAfterInitialiseRouterevent. This event is triggered each time the router object is created (which should only be once per process) and allows plugins to add their logic independent of the currently running application and especially independent of when the object is created. To show how this would be used in the future, the languagefilter plugin has been changed to use this new method.I first was sure that the languagefilter plugin would suffer from the problem I described above and that this would be a hidden bug. However I then noticed that the plugin "gets around this", by forcing the SiteRouter object upon plugin instantiation. I consider this pretty bad style...
Testing Instructions
Setup a multi language site and apply the patch. Notice that the URLs/behavior did not change.
Link to documentations
Please select:
Documentation link for docs.joomla.org:
No documentation changes for docs.joomla.org needed
Pull Request link for manual.joomla.org:
No documentation changes for manual.joomla.org needed