1

I am creating a custom component that will get data by json from another url. So far it works good but I am having some difficult trying to apply a SEF URL with a router.php file.

Example, current url from my component is this: index.php?option=com_mycomponent&id=333

The desired SEF URL (It will not be generated by Jroute, it just need to be read by joomla) is: website.com/alias-id

I only need the id to parse JSON. Alias is irrelevant.

The thing is... I cannot make the router.php work at all. My route never returns the values from my $vars array. It always says: Notice: Undefined variable: vars in [...]router.php on line 30.

I trying everything, but even if I try to put a exact var inside the ParseRoute function. (Like $vars[] = 30; ) the $vars output is still undefined.

I tried countless examples, I even tried using Jumi router.php as a example because I know that is a extension that will recognise the alias on frontend URL without any link on Jroute or menu id... but no luck.

Here is my current router.php. Because there is no query at all (and I have no idea where it comes from btw) I dindt used a $query var on BuildRoute function.

function myComponentBuildRoute()
    {
    $url = JURI::getInstance();

    $myid = end(explode('-', $url)); // gets the id from a /alias-id format
    $segments = array();
    $segments[] = $myid;

    return $segments;
}

    function myComponentParseRoute($segments)
    {
       $vars = array();


    $vars['jid'] = $segments[0];

    return $vars;
}

print_r($vars);

So maybe I am missing the logic on the router.php at all. Maybe it needs something from view. I am not even sure how I may use the $vars array after it finnaly pass. But then what I need to do to make a SEF url that Joomla will understand and will not give me a 404?

  • Check my answer below. It handles the router logic, view logic, and menu item logic for you. – Terry Carter May 19 '17 at 16:23
3

One thing is, that you're trying to create a router in the old way, it changed over time. Now router is a class. Second thing is, one of your component views have to be set as a front-page for a system. Without that, in most websites the default router will be com_content's router, so every wrong URL(url that don't have a proper menu item/parent item) will be handled by com_content and most likely return 404. If you want to handle SEF urls for a pages that don't have a proper menu item its a must.

Example component router

You can find a proper example router in the link below, but have in mind that in one of the next version of Joomla! the way you build the router will change again (if they get rid of the bugs).

https://github.com/joomla-extensions/weblinks/blob/master/src/components/com_weblinks/router.php

1

Create your URLs like this:

echo JRoute::_('index.php?option=com_mycomponent&Itemid='.Itemid.'&aliasid=alias-'.$id);

You will want to include the Itemid of your current menu item, makes things cleaner and helps you keep track of where you are. That can be found with:

$Itemid = JFactory::getApplication()->getMenu()->getActive()->id;

Try this for your router. It is a very simple router that looks for a url variable of aliasid and passes that value as a segment to the route parser. The route parser then writes that element to a variable. The variable can then be called using the application input method.

function myComponentBuildRoute(&$query)
{
    $app = JFactory::getApplication();
    $segments = array();
    if(isset($query['aliasid'])){
        $segments[] = $query['aliasid'];
        unset($query['aliasid']);
    }
    return $segments;
}


function myComponentParseRoute($segments)
{
    $vars = array();
    switch(count($segments))
    {
        case '1':
            $id = end(explode('-', str_replace(':','-',$segments[0])));
            $vars['id'] = $id;
            break;
    }
    return $vars;
}

Then to get the id inside your view just use the following:

$JInput = JFactory::getApplication()->input;
$this->id = $JInput->get('id','','int');
  • Thanks but this dont get me a website.com/alias-id, just a website.com/component/mycomponent/alias-id – user1967509 May 21 '17 at 11:10
  • Then it isn't pulling your Itemid. Echo the Itemid and see what it gives you. – Terry Carter May 21 '17 at 13:17
1

My original goal was creating a alias on frontend without any link using Jroute or menu id...

But after a lot of search it seems that Joomla cannot do that or if it can it may need some serious change on Router. So instead of messing with Joomla Router (I actually tried) I created a script on my plugin to create a menutype and to save on database a new menu item on that menutype for every page that I may want, that was the easiest way to have a working sef url for every item on listed on my JSON.

If anyone may want some hint about a router solution you may check this link: https://docs.joomla.org/Creating_a_System_Plugin_to_augment_JRouter

It is for j25 and I believe the router itself already changed a lot after this version, but it was the closest thing that I found that suggest how to revamp Joomla router, you may have some ideas about how to proceed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.