Skip to content
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

Missing retrieval of a specific router #22846

Closed
Giuse69 opened this issue Oct 28, 2018 · 4 comments
Closed

Missing retrieval of a specific router #22846

Giuse69 opened this issue Oct 28, 2018 · 4 comments

Comments

@Giuse69
Copy link
Contributor

Giuse69 commented Oct 28, 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

@mbabker
Copy link
Contributor

mbabker commented Oct 28, 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.

@Giuse69
Copy link
Contributor Author

Giuse69 commented Oct 28, 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

@mbabker
Copy link
Contributor

mbabker commented Oct 28, 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.

@Giuse69
Copy link
Contributor Author

Giuse69 commented Nov 1, 2018

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

@Giuse69 Giuse69 closed this as completed Nov 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants