For each site, it is very important that the CNC (Personally understandable URL) is in order for the search engines to “love” it very much and in order to write a keyword to the link.
For Joomla 1.5 there are a lot of components to create CNC. These components work by writing all the links in the database. This is not at all logical for a site with a large number of pages, because a lot of data is stored in the database. Even for these components, you need to write your own plugins, in order for it to write down the links of your component correctly.
In general, I have never been a supporter of these components. Only once I used, and I would not say that this is what I would like to use. Such a structure of CNC, I would call it "shamanism". And in order not to write such "shamanism", in Joomla 1.5 there are standard ways to create CNC for your components.
')
First, in the administrative part, in the configuration, you need to turn on the CNC. And all links output using
JRoute :: _ ('link') . We will consider the creation of CNC, for example, my component.
A little story about the component. I created this component to create a database of olympiad tasks with a verification system. I wrote it for my coursework. As you understand, this should be something of the type known
acm.timus.ru . In general, we have a database of olympiad algorithmic tasks in computer science. The person selects the task, sends the solution, looks at the result, has the ability to view the statistics of each task. But CNC for example, I will only build to view the task. If you have questions how to make for a larger amount, write. But in principle, this is done by analogy.
And so, our component is called
com_tasks . Now we create the
router.php file in the folder of our component. This file consists of two functions, this is the function
(Component name) BuildRoute is the function for building links, and
(Component name) ParseRoute is the function for recognizing links. We will consider these functions in turn.
(Component name) BuildRoute - link building function
Feature List:
function TasksBuildRoute(&$query)
{}
We have a link of the form:
/index.php?option=com_tasks&view=task&id=/{task_id}&Itemid=2 , our task is to make of it /
tasks/{ttask_id ___task_namet.htmlLet's see what is passed to us for link building, the
$ query array, for the task with
id = 1 .
Array (
[option] => com_tasks
[view] => task
[id] => 1
[Itemid] => 2
)
This function should return an array of parts of the link that we need between parts of Joomla then puts "/".
function TasksBuildRoute(&$query)
{
$segments = array();
if ($query[ 'view' ] == 'task' ) {
$segments[] = $query[ 'id' ]. '_' .$name;
unset($query[ 'view' ]); //
unset($query[ 'id' ]);
}
return $segments;
}
With this feature, our link has the form:
/component/tasks/1_.html .
As you can see, we did not get what we wanted. Because we got not quite clear, for us
/ component / . This word was written by Joomla in order to know which component to access. Notice that the
tasks in the link are the name of the component. If you make a menu item with the indication of our component, specify
alias to this item and write
Itemid = {id of our component} in the links during the output, then we will have what we need.
I made a menu item named
Tasks and
alias tasks with an indication of the component and received this link:
tasks / 1_.html . Here the
tasks are the alias of the menu item.
Let's go back to the view that we wrote in the build function. As you can see, we are deleting some elements of the array, because the elements that we do not delete will be substituted by Joomla as a parameter. For example, if you do not write
unset ($ query ['view']); then we would get a link like
/tasks/1_.html?view=task .
I also want to draw your attention that we have not yet pulled out the name of the task. We could do so that, for a given
$ query ['id'], we get a name in the database. But at the same time, if we have 100 references to tasks on one page, then we will have 100 requests to the database. And this is not a very logical step. In this case, we need to make a separate function to collect the names of all tasks. Create a
helper route for our component. To do this, create a folder
helpers in the folder of our component and in it a file
route.php .
jimport( 'joomla.application.component.helper' );
class TasksHelperRoute
{
public static $tasks = null ;
function getTasks()
{
if (self::$tasks) return self::$tasks;
$db = &JFactory::getDBO();
$query = "SELECT `id`, `name` FROM `#__tasks`" ;
$db->setQuery($query);
$res = $db->loadObjectList();
foreach ($res as $r) {
self::$tasks[$r->id] = $r->name;
}
return self::$tasks;
}
}
Now we will connect this file, call the function, and substitute the name of the task.
include_once(JPATH_ROOT.DS. 'components' .DS. 'com_tasks' .DS. 'helpers' .DS. 'route.php' );
function TasksBuildRoute(&$query){
$segments = array();
$tasks = TasksHelperRoute::getTasks();
if ($query[ 'view' ] == 'task' ) {
$segments[] = $query[ 'id' ]. '_' .$tasks[$query[ 'id' ]];
unset($query[ 'view' ]);
unset($query[ 'id' ]);
}
return $segments;
}
Now we have a link of the form
/tasks/1_a_plus_b.html . We coped with this task.
Due to the fact that we have a static variable in the
helpere class, the request to the database will be only once. In the same
helpere, you can handle the name: take
away spaces, make all the characters in lower case, etc.
In order for the CNC to work, you need to not only build, but also parse the links. For this you need to "introduce" this link with the system.
(Component name) ParseRoute - function to recognize the link
This function is described as:
function TasksParseRoute($segments)
{}
The
$ segments array is exactly the same as we returned to the build function. It is necessary to return the
$ vars array , the same as we received in the build function.
In this case, we will receive the following
$ segments :
Array (
[0] => 1_a_plus_b
)
In this example
view , we should always have a
task , we do not have links of another kind. And now we need to get only
{id} from the line
{id} _ {name } . This is done simply by using string functions.
function TasksParseRoute($segments)
{
$vars[ 'view' ] = 'task' ;
$vars[ 'id' ] = substr($segments[0], 0, strpos( '_' , $segments[0])+1);
return $vars;
}
Well, in general, we introduced the system with our links.
We looked at one of the easiest examples. More difficult you can do yourself, based on this example. Waiting for questions, suggestions for improvement, criticism.