📜 ⬆️ ⬇️

SEO URL support in Joomla 3 MVC component

For the catalog component you need to organize beautiful links. I will describe on a living example what needs to be done for this. The article is written on the go. I write the code, I test, if everything works, I finish the 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:
')
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 the 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; } 

URL generation in the 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.

Required to substitute the URL prescribed by the user.
This url is stored in the directory table.

Add another function that will pull out 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 function for parsing 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 / catalog / test_alias link, the necessary page will open.

The function for generating the url is as follows:

 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.

Thank!

Source: https://habr.com/ru/post/249987/


All Articles