In Joomla, for creating SEF URLs in joomla component you need to make your router file which has very important role in each and every component to allow SEF URLs.
Let's assume that your component url is like this:-
http://www.mysite.com/index.php?option=com_joomcomponent&view=article&id=10&catid=40&Itemid=50
and you want to convert your URL like this:-
http://www.mysite.com/itemname/40/10
so first you need to send information to system that few parameters of text are URLs and need to be convert, and explaining the system how to convert URLs.
Applying JRoute::_
You will need to change URL generating code so that it applies JRoute::_ before outputting the URL to support SEF URLS like this:
echo JRoute::_('index.php?view=article&id=10&catid=40');
Its possible to leave the option and itemid parameters as option defaults name of component and Itemid defaults to current menu id if the user turns off SEF URLs,JRoute::_ make non-SEF URLs without changes in code.
Next you need to create a router file where you need to write two functions "BuildRoute" and "ParseRoute" that convert system URLs to and from SEF URLs.One is reponsible for building the url and other is for parsing it. This file needs to be placed at /components/com_joomcomponent/router.php.
The file router.php should be in the site area of your component. It is not used on admin/backend pages.
The first function BuildRoute($qry) written inside component router file, must convert an URL parameters array into segments array that will make the SEF URL. Conversion process is given below:
Component BuildRoute Function:-
http://www.mysite.com/index.php?option=com_yourcomponent&view=article&id=10&catid=40&Itemid=50
Component calls the JRoute::_ and it will become
$query = array('view' => 'article', 'id' => 10 'catid' => 40)
JRouter passes a $qry array to the component BuildRoute function. This function will return the ordered array of segments
BuildRoute Function will return segments as an array like this:-
$segments = array(40, 10);
which will used for url display.
http://www.example.com/example-menu-item/40/10
example:-
function [componentname]BuildRoute(&$query)
{
$segments = array();
if (isset($query['view']))
{
$segments[] = $query['view'];
unset($query['view']);
}
if (isset($query['id']))
{
$segments[] = $query['id'];
unset($query['id']);
};
return $segments;
}
Component ParseRoute function:-
The second function, [componentname]ParseRoute($segments), must transform an array of segments back into an array of URL parameters.
http://www.mysite.com/itemname/20/1 with joomla internal route parsing
$segments = array(20, 1);
ParseRoute function get segment array as a parameters and convert liek this:-
$query = array('view' => 'article', 'id' => 1, 'catid' => 20)
for example:-
function [componentname]ParseRoute($segments)
{
$vars = array();
switch($segments[0])
{
case 'categories':
$vars['view'] = 'categories';
break;
case 'category':
$vars['view'] = 'category';
$id = explode(':', $segments[1]);
$vars['id'] = (int) $id[0];
break;
case 'article':
$vars['view'] = 'article';
$id = explode(':', $segments[1]);
$vars['id'] = (int) $id[0];
break;
}
return $vars;
}
The two functions must act in such a way that the original URL can be reconstructed it works like encoding and decoding.
0 Comment(s)