
SEO URL support in MVC component Joomla 3
- From the sandbox
- Tutorial
For the catalog component, you need to organize beautiful links. I will describe with a living example what needs to be done for this. The article is written on the go. I am writing code, testing, if everything works, I am adding an article.
First you need to create router.php in the component folder (/components/com_catalog/router.php).
Add a function to it that will generate the url:
The second function will parse url into its component parts:
Generating a URL in a component:
JRoute :: _ ('index.php? View = item & id ='. $ Row-> id);
Now the component understands links of the form / catalog / item / 1.
This is an example from the documentation. We modify it for a more interesting task.
It is required to substitute the URL prescribed by the user.
This url is stored in the directory table.
Add another function that will pull the element:
And so now our parsing function will look like this:
When you click on the link / catalog / test_alias, the desired page opens.
The function for generating url has become like this:
Now JRoute :: _ ('index.php? View = item & id = 1'); will give us the url / catalog / test_alias we need.
Thanks!
First you need to create router.php in the component folder (/components/com_catalog/router.php).
Add a function to it that will generate the url:
function catalogBuildRoute(&$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;
}
The second function will parse url into its component parts:
function catalogParseRoute($segments)
{
$vars = array();
switch($segments[0])
{
case 'catalog':
$vars['view'] = 'catalog';
break;
case 'item':
$vars['view'] = 'item';
$id = explode(':', $segments[1]);
$vars['id'] = (int) $id[0];
break;
}
return $vars;
}
Generating a URL in a component:
JRoute :: _ ('index.php? View = item & id ='. $ Row-> id);
Now the component understands links of the form / catalog / item / 1.
This is an example from the documentation. We modify it for a more interesting task.
It is required to substitute the URL prescribed by the user.
This url is stored in the directory table.
Add another function that will pull the element:
function getCatalogItemByRow($row, $value){
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('id, url');
$query->from($db->quoteName('#__catalog'));
$query->where($db->quoteName($row)." = ".$db->quote($value));
$db->setQuery($query);
return $db->loadRow();
}
And so now our parsing function will look like this:
function catalogParseRoute($segments)
{
$vars = array();
$vars['view'] = 'catalog';
if($segments[0]!="catalog"){
$item = getCatalogItemByRow("url",$segments[0]);
if(isset($item['1']) && $item['1']) {
$vars['view'] = 'item';
$vars['id'] = (int) $item['0'];
}
}
return $vars;
}
When you click on the link / catalog / test_alias, the desired page opens.
The function for generating url has become like this:
function catalogBuildRoute(&$query)
{
$segments = array();
unset($query['view']);
if (isset($query['id']))
{
$id = (int) $query['id'];
if($id){
$item = getCatalogItemByRow("id",$id);
$segments[] = $item['1'];
unset($query['id']);
}
}
return $segments;
}
Now JRoute :: _ ('index.php? View = item & id = 1'); will give us the url / catalog / test_alias we need.
Thanks!