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.
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
Joomla 3.8.x
Labels |
Added:
?
|
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
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.
Labels |
Added:
J3 Issue
|
Status | New | ⇒ | Closed |
Closed_Date | 0000-00-00 00:00:00 | ⇒ | 2018-11-01 17:14:11 |
Closed_By | ⇒ | Giuse69 |
Thanks mbabker. I am closing this issue for now and will investigate in other components hooking to core Joomla router.
getRouter
returns aJoomla\CMS\Router\Router
instance. It's not returning "all component routers"; it is returning the core Router object for that application. Interesting enough, only theJoomla\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 theJoomla\CMS\Router\Router
API because there is more to the routing process than simply executing the code in a component's router class.