I am trying to write a custom component router. I have these admin and site uri structures: http://localhost/administrator/index.php?option=com_{component_name}&... and http://localhost/index.php/{language_code}/{an_alias}/.... Is there a way to get these uri structures programmatically. The admin part is okay, but by the site part I am lost. I can add any menupoint with a generated alias, which is not predictable. Is there a way to get this info from the joomla site router or enforce a constraint on the menupoint?

Currently I have a com_example/views/example/tmpl/default.xml:

<?xml version="1.0" encoding="utf-8"?>
    <layout title="COM_CANTEEN_TITLE">

which is enough to add a site menupoint, which points to the component. It would be nice to add multilingual menupoint title and alias to this, so it would not be possible to add a menupoint with a different alias. Actually I don't want to follow the MVC structure by the component, I'd like to route and handle requests from the entry, let's call it com_example/example.php. It would be nice to have this template xml file in the com_example/Example/presentation/menupoints/ if that is possible.

1 Answer 1


It is possible to use the router, which finds the component to create a base uri for the component.

$application = JFactory::getApplication();
$router = $application->getRouter();
$componentUri = $router->build('index.php?option=com_example');

This will return /administrator/index.php?option=com_example by admin pages and something like /index.php/en/alias by site pages if you defined a menu point for the component with an alias. A drawback that this solution does not work properly if I access the site using the /index.php/en/?option=com_example URI, so without using a menu point. It returns something like /index.php/en/component/example/, which leads to a 404 page. This is not a real issue as long as we are trying to access the site pages with the alias containing URI.

Another alternative way to use the JRoute::_('index.php?option=com_example') code, which does the exact same thing.

By default there is a legacy component router instantiated by the app, but it is not necessary to use it. I decided to write a custom router instead and I annotated my controllers and actions with the necessary info. So the approach is somewhat similar to java rest webservices.

I did not have time to find a solution for moving the xml file, it is not a priority now.

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.