Component router.php to modify URI before component alias ?

651 views
Skip to first unread message

Patrick Hertling

unread,
Nov 1, 2013, 3:03:47 PM11/1/13
to joomla-de...@googlegroups.com
Hi,
Normal Joomla URL for a component with SEF activated and custom router.php look like this:

www.example.com/<component_name_menu_alias>/[segment_1]/[segment_2]/.../[segmen_n]

Normally, the router.php for every component allows you to produce custom URL for it. I want to customize it in the following way:

www.example.com/[segment_x]/<component_name_menu_alias>/[segment_1]/...

Do you know what I mean? Is that possible? If yes, can you plesae tell me how please?

Greetings, Patrick

Cliff Ford

unread,
Nov 2, 2013, 4:02:40 AM11/2/13
to joomla-de...@googlegroups.com
I have done this (in 2.5) with an onAfterInitialise plugin that attaches build and parse rules like so:

    function onAfterInitialise()
    {
        ...
        $app = JFactory::getApplication();
        // Get the router
        $router = $app->getRouter();
        // Attach the callbacks to the router to add/remove XX-YYY/zz from path
        $router->attachBuildRule(array($this, 'buildRule'));
        $router->attachParseRule(array($this, 'parseRule'));
    }

    public function parseRule (&$router, &$uri)
    {
        your code
    }
    public function buildRule (&$router, &$uri)
    {
        your code
    }

Hope this helps

Cliff
--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.
To post to this group, send an email to joomla-de...@googlegroups.com.
Visit this group at http://groups.google.com/group/joomla-dev-general.
For more options, visit https://groups.google.com/groups/opt_out.

Patrick Hertling

unread,
Nov 4, 2013, 1:48:39 PM11/4/13
to joomla-de...@googlegroups.com
Thank you so much for your time,

I 've written the system plugin with the two functions and attached them to the router, I've checked out the $url and $router parameters in the debugger and I know how URL rewriting on apache servers works through the .htaccess and the web.config file.

The $url object is quite simple, but the router object is very complex. I was expecting to do something similar to what URL rewriting does with regular expressions, but I have no idea where(if that is what I have to do).

Since I want to change the secuence of the URL parameters I guess I have to add rules somewhere. But instead of doing it directly in the .htaccess file, I would like to do it in the component package since it goes all together.

Could I have some help once more or some documentation? Thank you very much

Cliff Ford

unread,
Nov 4, 2013, 5:39:23 PM11/4/13
to joomla-de...@googlegroups.com
Here is some documentation that you may find helpful (or confusing) :

http://docs.joomla.org/Creating_a_System_Plugin_to_augment_JRouter

You don't need to tinker with .htaccess, or anything else. For my extension the requirement was to add segments to the path so that it looked like this: http://mysite.com/XX-YYY/zz/example.html where 'example' is a view name and XX-YYY is a branch code that varies with country and zz is a language code. The initial values of XX-YYY and zz come either from geolocation or the url. It is perhaps easier to understand the build rule - wherever you have a JRoute call in your code:

JRoute::_('index.php?option=com_myextension&view=myview')

the build rule gets called. In my case it looke like this:


    public function buildRule (&$router, &$uri)
    {
        if ($uri->getVar('option') == 'com_sciops') {
            $input = new JInput($_COOKIE);
            $branch_code = $input->get('branch_code', 'AA-SCI', 'string');
            if ($branch_code != 'AA-SCI') {
                $language_code = $input->get('language_code', 'en', 'string');
                $uri->setPath($uri->getPath().'/'.$branch_code.'/'.$language_code.'/');
            }
        }
        return;
    }
So in the output page the links have XX-YYY/zz in the path (in the example above AA-SCI is a default that is left out). If you follow execution with the debugger there is one call for each link in the output page. We keep the XX-YYY and zz values in cookies.

On input (one call per page load), to parse a url we need to turn the XX-YYY/zz values back into parameters and remove them from the path. This is my code to do that:


    public function parseRule (&$router, &$uri) {
        $vars = array();
        $path = $uri->getPath();
        if (preg_match('/[A-Z]{2}[-]{1}[A-Z]{2,3}\/[a-z]{2}/i', $path) > 0) {
            $parts = explode('/', $path);
            $vars['url_branch'] = array_shift($parts);
            $vars['url_language'] = array_shift($parts);
            $uri->setPath(implode('/' , $parts));
        } else if (preg_match('/[a-z]{2}\//', $path) > 0) {
            $parts = explode('/', $path);
            $vars['url_branch'] = 'AA-SCI';
            $vars['url_language'] = array_shift($parts);
            $uri->setPath(implode('/' , $parts));
        }
        return $vars;
    }

so url_branch and url_language get returned in the vars array for use elsewhere in the program. They end up in the Request so you can access them with JFactory::getApplication()->input->get();

I hope this gives some extra insight.

Cliff
Reply all
Reply to author
Forward
0 new messages