J3 Issue ?
avatar Giuse69
Giuse69
28 Oct 2018

Is your feature request related to a problem? Please describe.

Currently JFactory::getApplication()->getRouter() returns ALL the routers registered in the application, it should be called getRouters().
I didnt find a clean way to retrieve the router associated to a specific component, to invoke its method like building a SEF according to that component.

Describe the solution you'd like

Either having a way to invoke getRouter with the component name like getRouter(string componentName) or another function, since I think we don't want to change name of getRouter() into getRouters() for backward compatibility

Additional context

Joomla 3.8.x

avatar Giuse69 Giuse69 - open - 28 Oct 2018
avatar joomla-cms-bot joomla-cms-bot - change - 28 Oct 2018
Labels Added: ?
avatar joomla-cms-bot joomla-cms-bot - labeled - 28 Oct 2018
avatar mbabker
mbabker - comment - 28 Oct 2018

getRouter returns a Joomla\CMS\Router\Router instance. It's not returning "all component routers"; it is returning the core Router object for that application. Interesting enough, only the Joomla\CMS\Router\SiteRouter subclass is actually aware of the component router classes, which basically act as hooks in the main routing system.

If you really need a component specific router, JFactory::getApplication()->getRouter('site')->getComponentRouter() will do the trick (it MUST be this because as I said, only the SiteRouter is aware of the component concept). But really, you shouldn't be trying to build or parse URIs using the component routers directly; always use the Joomla\CMS\Router\Router API because there is more to the routing process than simply executing the code in a component's router class.

avatar Giuse69
Giuse69 - comment - 28 Oct 2018

Thanks mbabker, I said "all the components" since a made a var_dump of the result of getRouter with XDebug and saw there all component routers, not just core router.
I add some more detail to the scenario: adopt a third party component XXX that overrides Joomla! com_content (one of the various CCK available), create a menu item for XXX of type blog view, then on the front end click on an article and look at the resulting URL: the URL is made with com_content and not with com_XXX, so there are problems in building SEF URLs since the native router is invoked and not the XXX one. So my attempt is: get current menu item's component, that is XXX and not com_content, and invoke SEF building forcing the app to use XXX router and not joomla one.
Did I explain myself clearly?
thanks a lot

avatar mbabker
mbabker - comment - 28 Oct 2018

When you dump an instance of Joomla\CMS\Router\SiteRouter (which is the "core router" for the frontend application), it is composed of all the component routers. From the outside, you shouldn't be trying to build or parse URLs by triggering the component routers directly, you should always be using the JRoute::_() facade to create URLs and Joomla\CMS\Router\Router::parse() to read them.

Joomla\CMS\Router\Router uses a hook based system called "rules" to determine how to handle URLs. In the case of the Joomla\CMS\Router\SiteRouter subclass, the bulk of these hooks are driven by the component routers and other configurations based on the classes found in the Joomla\CMS\Component\Router namespace. So these CCK components that are adding new views to existing components should also be adding appropriate hooks to the routing API to handle "intercepting" that process to make sure things are processed correctly.

Any user should never need to dig into the router internals and explicitly retrieve a specific component router and manually build URLs. Any call to Joomla\CMS\Router\Router::build() (which is what the JRoute::_() facade triggers basically) should always produce the right result and if it doesn't then that is a bug with those components.

avatar brianteeman brianteeman - change - 30 Oct 2018
Labels Added: J3 Issue
avatar brianteeman brianteeman - labeled - 30 Oct 2018
avatar Giuse69 Giuse69 - change - 1 Nov 2018
Status New Closed
Closed_Date 0000-00-00 00:00:00 2018-11-01 17:14:11
Closed_By Giuse69
avatar Giuse69
Giuse69 - comment - 1 Nov 2018

Thanks mbabker. I am closing this issue for now and will investigate in other components hooking to core Joomla router.

avatar Giuse69 Giuse69 - close - 1 Nov 2018

Add a Comment

Login with GitHub to post a comment