Removing id variables in joomla v1.5 router

I have custom Joomla(v1.5) component and currently working with component's router. The problem is I can't remove id numbers from SEF url. I get:

http://myaddress.com/componentalias/17-city-alias/130-item-alias

What I want to get:

http://myaddress.com/componentalias/city-alias/item-alias

Take a look at router.php methods below:

function ComponentnameBuildRoute(&$query) {

    $segments = array();

    if(isset($query['city_id'])){               
        $segments[] = $query['city_id'];                    
        unset($query['city_id']);
    }

    if(isset($query['item_id'])){
        $segments[] = $query['item_id'];          
        unset($query['item_id']);
    }


    if(isset($query['task'])){
        switch($query['task']){
            case 'pay':
                $segments[] = JText::_('payment');
                unset($query['task']);
                break;  
        }
    }

    unset($query['view']);

    return $segments;
}
/*
 * Function to convert a SEF URL back to a system URL
 */
function ComponentnameParseRoute($segments) {

    $var = array();

    if(isset($segments[0])){
        $cityData = explode(':',$segments[0]);
        if(isset($cityData[0])){
            $vars['city_id'] = $cityData[0];
        }
    }

    if(isset($segments[1])){
        $itemData = explode(':',$segments[1]);
        if(isset($itemData[0])){
            $vars['item_id'] = $itemData[0];
        }
    }

    if(isset($segments[2])){
        switch($segments[2]){
            case JText::_('payment'):
                $vars['task'] = 'pay';
                break;  
        }
    }


    return $vars;

}

Any ideas? Your help would be appreciated.

Answers


The best place to start with your router.php file is reading this article (it's a bit dated but still good) and then reviewing the com_content's router.php file (components/com_content/router.php). You will notice that articles do achieve what you want so best to look at working code and got from there.

Longer answer:

  1. You can only get rid of the item ID variables in a path to a content element if a menu item exists that points directly to the item, otherwise there is no way to find the item.

  2. SEF URLs in Joomla! 1.5 etc are made from the alias' of the individual elements

eg. If I have this menu structure:

Recipes                (The menu)
-- Seafood             (<-- Category blog where category alias is `seafood` )
  -- Grilled Snapper      (<-- Recipe Item where item alias is `grilled-snapper` )
-- 'Other category'    (<-- Another Category blog )
Full ID Removal

In the case where you're building the SEF URL for a recipe you can build the route by looking for the menu item it might appear in by getting the site menu $menu = &JSite::getMenu(); and comparing the query id in the current menu item against id value in the $query array passed in.

If you have a match you can build the segments up using the alias from the menu path and the recipe alias. (And reverse the process in your ParseRoute($segments) method).

So, from this example above you could build a SEF URL to the Grilled Snapper recipe that looks something like: recipes/seafood/grilled-snapper.

Partial ID Removal

Now say you also have another recipe (e.g. 'Garlic Prawns' alias garlic-prawns) that isn't directly linked to a menu but will appear in the 'Seafood' category blog page. In this situation you would end up with recipes/seafood/2:garlic-prawns

If you don't have a match (like the Garlic Prawns), you can build up partial match if your component has list views like category blogs or in our example Recipe category pages... Essentially in this case you look at the current menu item and determine if it's a list/category view that would contain the content item.

If it is then the path to the category/list view form you initial segments, but as there is no menu item for the article you will still have to use the ID of the item in the last element of the URL.

No Menu Item for content item or a list/category view

When the content item is being linked to directly (e.g. from an article, module or search result) and there are no menu items that point to it or could contain it then you can still create a URL without id's in it but you will be providing the path in the form of direct component access URL.

eg. /component/recipes/recipe/ice-cream-sundae where recipes is the name of the component, recipe is the view and ice-cream-sundae is the alias of the article.


Need Your Help

ld: framework not found com.apple.mobileicons

iphone ios xcode

I am currently developing my first iOS app which i have tested on simulator till date. Yesterday my developer account was approved and with a mammoth amt of excitement, I tried to deploy my app on ...

Search An Array Consisting of Sub-Arrays For the Largest Number and Return in a New Array

javascript arrays loops for-loop

I am working on a coding challenge to take a given array which consists of sub-arrays, search for the largest number in each sub-array, and finally return a new array consisting only of the largest