0

i have written a router.php for creating a custom joomla url. The router gives me the following url :

http://localhost/project_name/menu-item-alais/article-alias

But i wan to remove the menu-item-alias and make the url as follows :

http://localhost/project_name/article-alias

I tried to unset the Itemid but it gives me 404 error when i open the detail page.

Below is the router.php

<?php
/**
* @version      $Id: router.php 14401 2010-01-26 14:10:00Z louis $
* @package      Joomla
* @copyright    Copyright (C) 2005 - 2010 Open Source Matters. All rights reserved.
* @license      GNU/GPL, see LICENSE.php
* Joomla! is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/

function ContentBuildRoute(&$query)
{
    $segments = array();
    $menu = &JSite::getMenu();
    if (empty($query['Itemid'])) 
    {
       $menuItem = &$menu->getActive();
    } 
    else 
    {
       $menuItem = &$menu->getItem($query['Itemid']);
    }
    $mView  = (empty($menuItem->query['view']))? null : $menuItem->query['view'];
    $mCatid = (empty($menuItem->query['catid']))? null : $menuItem->query['catid'];
    $mId    = (empty($menuItem->query['id']))? null : $menuItem->query['id'];

if(isset($query['task'])) 
{
    return $segments;
}

if(isset($query['view']))
{
    $view = $query['view'];
    if(empty($query['Itemid'])) 
    {
        $segments[] = $query['view'];
    }
    unset($query['view']);
};

if (($mView == 'article') and (isset($query['id'])) and ($mId == intval($query['id']))) 
{
    unset($query['view']);
    unset($query['catid']);
    unset($query['id']);

}

if (isset($view) and ($view == 'section' && !empty($query['Itemid']))) 
{
    if (($mView != 'section') or ($mView == 'section' and $mId != intval($query['id']))) 
    {
        $segments[] = 'section';
        unset($query['Itemid']);
    }
}

if (isset($view) and $view == 'category') 
{
    if ($mId != intval($query['id']) || $mView != $view) 
    {
        $temp = explode(':',$query['id']);
        if(count($temp) > 1)
        {
            $query['id'] = $temp[1];
        }

        $segments[] = $query['id'];
    }
    unset($query['id']);
}

if (isset($query['catid'])) 
{

    if ((($view == 'article') and ($mView != 'category') and ($mView != 'article') and ($mCatid != intval($query['catid'])))) 
    {
        $temp = explode(':',$query['catid']);
        if(count($temp) > 1)
        {
            $query['catid'] = $temp[1];
        }

        $segments[] = $query['catid'];
    }
    unset($query['catid']);
};

if(isset($query['id'])) 
{
    if (empty($query['Itemid'])) 
    {
        $temp = explode(':',$query['id']);
        if(count($temp) > 1)
        {
            $query['id'] = $temp[1];
        }

        $segments[] = $query['id'];
    } 
    else 
    {
        if (isset($menuItem->query['id'])) 
        {
            if($query['id'] != $mId) 
            {
                $temp = explode(':',$query['id']);
                if(count($temp) > 1)
                {
                    $query['id'] = $temp[1];
                }

                $segments[] = $query['id'];
            }
        } 
        else 
        {
            $temp = explode(':',$query['id']);
            if(count($temp) > 1)
            {
                $query['id'] = $temp[1];
            }

            $segments[] = $query['id'];
        }
    }
    unset($query['id']);
};

if(isset($query['year'])) 
{

    if(!empty($query['Itemid'])) 
    {
        $segments[] = $query['year'];
        unset($query['year']);
    }
};

if(isset($query['month'])) 
{

    if(!empty($query['Itemid'])) 
    {
        $segments[] = $query['month'];
        unset($query['month']);
    }
};

if(isset($query['layout']))
{
    if(!empty($query['Itemid']) && isset($menuItem->query['layout'])) 
    {
        if ($query['layout'] == $menuItem->query['layout']) 
        {

            unset($query['layout']);
        }
    }
     else 
     {
        if($query['layout'] == 'default') 
        {
            unset($query['layout']);
        }
    }
};

return $segments;
}

function ContentParseRoute($segments)

{
    $vars = array();

$menu =& JSite::getMenu();
$item =& $menu->getActive();
$db =& JFactory::getDBO();
$count = count($segments);


if(!isset($item))




{
    $vars['view']  = $segments[0];
    $vars['id']    = $segments[$count - 1];
    if($vars['view'] == 'article')
    {
        $query = 'SELECT id FROM #__content WHERE alias = '.$db->Quote($vars['id']);
    } elseif($vars['view'] == 'category') {
        $query = 'SELECT id FROM #__categories WHERE section > 0 && alias = '.$db->Quote($vars['id']);
    }
    $db->setQuery($query);
    $vars['id'] = $db->loadResult();
    return $vars;
}

switch($item->query['view'])
{
    case 'section' :
    {
        if($count == 1) {
            $vars['view'] = 'category';

            if(isset($item->query['layout']) && $item->query['layout'] == 'blog') {
                $vars['layout'] = 'blog';
            }
        }

        if($count == 2) {
            $vars['view']  = 'article';
            $vars['catid'] = $segments[$count-2];
        }

        $vars['id']    = $segments[$count-1];

    } break;

    case 'category'   :
    {
        $vars['id']   = $segments[$count-1];
        $vars['view'] = 'article';

    } break;

    case 'frontpage'   :
    {
        $vars['id']   = $segments[$count-1];
        $vars['view'] = 'article';

    } break;

    case 'article' :
    {
        $vars['id']   = $segments[$count-1];
        $vars['view'] = 'article';
    } break;

    case 'archive' :
    {
        if($count != 1)
        {
            $vars['year']  = $count >= 2 ? $segments[$count-2] : null;
            $vars['month'] = $segments[$count-1];
            $vars['view']  = 'archive';
        } else {
            $vars['id']   = $segments[$count-1];
            $vars['view'] = 'article';
        }
    }
}

$alias = explode(':', $vars['id']);
if((int) $alias[0] > 0)
{
    $vars['id'] = $alias[0];
} 
else 
{
    if(count($alias) > 1)
    {
        $vars['id'] = $alias[0].'-'.$alias[1];
    }

    if($vars['view'] == 'article')
    {
        //$data=str_replace('-', ' ', $vars['id']);
        $query = 'SELECT id FROM #__petitions_signataires WHERE ref = '.$db->Quote($vars['id']);
    } 
    elseif($vars['view'] == 'category') 
    {
        $query = 'SELECT id FROM #__categories WHERE section > 0 && alias = '.$db->Quote($vars['id']);
    }
    $db->setQuery($query);
    $vars['id'] = $db->loadResult();
    $vars['latestnews'] = 'latest';
    $vars['detailslink'] = 'link';

}

return $vars;
}
?>

And here is the Jroute link :

<a href="'.JRoute::_('index.php?option=com_content&view=article&id='.$row->id.':'.$row->ref.'&Itemid=3').'">'.$row->pet_content.'</a>
1

If you are only trying to route directly to articles you can create a hidden menu in the backend and the create Single Article menu items on that menu. This will allow you to access your articles directly to the project name if that is what you are looking to do. No need for a special router.

From the backend:

  1. Menus > Manage > Add New Menu
  2. Enter Hidden Menu (or whatever you want) for all three fields
  3. Menus > Hidden Menu > Add New Menu Item
  4. Create Menu Item Typs System > Text Separator
  5. Name the New Menu item after your project
  6. Create a New Menu Item > Articles > Single Article
  7. Choose your article from the list
  8. Set the parent menu item to the project menu item separator you just created.
  9. Test it by accessing http://yoursite.tld/project-name/article-name

Hope this helps and saves you some work.

0

That is a good way to get the job done. You can also download a free extension called Advanced Module Manager. This gives you a much more granular way to control where your content appears. You can assign modules to your articles by category, tag or even article ID.

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.