Actions

Difference between revisions of "Supporting SEF URLs in your component"

From Joomla! Documentation

m (Preparing Your Data for Routing)
 
(7 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{version|1.5,2.5,3.0}}
+
{{version/tutor|2.5,3.1}}
 
{{:Search Engine Friendly URLs}}
 
{{:Search Engine Friendly URLs}}
 
In Joomla!, each [[component]] is responsible for handling its own SEF URLs. Therefore, as the [[Developers|developer]] of a component, you will have to create your own '''router''' to allow your component to use SEF URLs.
 
In Joomla!, each [[component]] is responsible for handling its own SEF URLs. Therefore, as the [[Developers|developer]] of a component, you will have to create your own '''router''' to allow your component to use SEF URLs.
Line 56: Line 56:
  
 
== Preparing Your Data for Routing ==
 
== Preparing Your Data for Routing ==
 +
 +
Clearly, any URL format needs to contain some kind of information that identifies the data you want to show. If your system URLs look like <tt><nowiki>http://www.example.com/index.php?option=com_yourcomponent&view=article&id=1&catid=20&Itemid=50</nowiki></tt>, that information is currently the <tt>id</tt> URL parameter (<tt>id=1</tt>). You probably want your SEF URLs to contain a textual description of the data they point to. In Joomla!, this is usually done by giving your users a way to enter an '''alias''' to be used in the URL.
  
 
=== The Alias ===
 
=== The Alias ===
  
The first step is the generation of the so called alias. The alias is used in the URL instead of the title (the alias is the text you want to have in the URL). The alias has to be URI safe, which means accented UTF­8 characters are replaced by their ASCII­7 equivalents, white spaces by hyphens, etc.
+
Even if your users can enter an alias, they might not do so, leaving the generation of a sensible alias up to your component. If your data has a title field, you can use that as a candidate for the alias (like the core [[Content]] component does).
  
The alias can be defined by the user, but you should ensure that the above requirements for a URL safe alias are met. A good way to do so is to use the JTable::check() method during the save process. Have a look at this example code:
+
Considering that the alias will be used in URLs, it has to be URL safe. Joomla! provides a method making arbitrary strings URI safe, which includes replacing accented UTF­8 characters by their ASCII­7 equivalents, white spaces by hyphens, etc. Whether the user entered the alias or a candidate has been chosen automatically, you should ensure that the above requirements for a URL safe alias are met. A good place for implementing this, if you are using <code>JTable</code>, is the <code>JTable::check()</code> method, which is called during the save process. Have a look at this example code:
  
 
<source lang="php">
 
<source lang="php">
Line 67: Line 69:
 
function check()
 
function check()
 
{
 
{
     jimport( 'joomla.filter.output' );
+
     jimport('joomla.filter.output');
     if(empty($this->alias)) {
+
     if (empty($this->alias))
 +
    {
 
    $this->alias = $this->title;
 
    $this->alias = $this->title;
 
     }
 
     }
Line 78: Line 81:
 
</source>
 
</source>
  
If the alias field is empty the title will be used as alias. Then the alias will be made URLSafe using the <code>JFilterOutput::stringURLSafe()</code> method.
+
If the alias field is empty the title is used as alias. Then the alias is made URL safe using the <code>JFilterOutput::stringURLSafe()</code> method.
  
 
=== The Slug ===
 
=== The Slug ===
  
Continuing with the same example, the "slug" - "1­:welcome­-to­-joomla" has two parts. The first part is the article identifier (id) and the second is the alias. They are separated by a colon. These two elements were combined during the database query in the model:
+
A '''slug''' is used to minimise the amount of code you need to support SEF URLs. It consists of the numerical identifier (<tt>id</tt>) your system URLs used, a colon (<tt>:</tt>), and the alias you created as described above.
 +
 
 +
Consider a SEF URL for an [[Article]] with <tt>id</tt> 1 and title "Welcome to Joomla!". The automatically generated alias for this article is <tt>welcome-to-joomla</tt>, and the slug becomes <tt>1­:welcome­-to­-joomla</tt>. In the [[Content]] component, the two elements are combined during the database query in the model (<code>a</code> represents <code>#__content</code>):
  
 
<source lang="php">$query = 'SELECT a.*, '.
 
<source lang="php">$query = 'SELECT a.*, '.
Line 88: Line 93:
 
         /*...*/;</source>
 
         /*...*/;</source>
  
After this step the slug is used instead of the id.
+
The advantage of this method of creating a slug is that you can simply use the slug as a drop-in replacement for the id in most places. For example, you don't need to check for and remove the colon and the alias from the request data manually: if you use [[Retrieving request data using JInput|JInput]]'s <code>int</code> (integer) filter, it will do that automatically.
  
 
== Routing URLs ==
 
== Routing URLs ==
  
The <code>JRoute::_</code> method translates the internal Joomla! URL to a custom URL. <tt>JRoute</tt> has three parameters and its prototype is:
+
The <code>JRoute::_</code> method translates the internal Joomla! URL to a custom URL. <code>JRoute::_</code> has three parameters and its prototype is:
  
<source lang="php">JRoute::_( $url, $xhtml = true, $ssl=null );</source>
+
<source lang="php">JRoute::_($url, $xhtml = true, $ssl = null);</source>
  
 
Where:
 
Where:
Line 104: Line 109:
 
The most important parameter is <code>$url</code>. A call to this method might look like:
 
The most important parameter is <code>$url</code>. A call to this method might look like:
  
<source lang="php">JRoute::_( 'index.php?view=article&id='.$row->slug );</source>
+
<source lang="php">JRoute::_('index.php?view=article&id=' . $row->slug);</source>
  
 
<code>$row-­>slug</code> is the value that was generated in step 2 from a combination of id and title alias.
 
<code>$row-­>slug</code> is the value that was generated in step 2 from a combination of id and title alias.
  
Another advantage of using JRoute is that the router now handles $option (the component name) and the $Itemid (the menu item ID). The component itself doesn’t have to know its name ($option) or the active menu item ($Itemid) like it did in previous version of Joomla!.
+
Another advantage of using <code>JRoute::_</code> is that the router now handles <code>$option</code> (the component name) and the <code>$Itemid</code> (the menu item ID). The component itself doesn't have to know its name (<code>$option</code>) or the active menu item (<code>$Itemid</code>) like it did in previous version of Joomla!.
  
It is important that you think about the sequence of the URL parameter in this stage. This will be more clear when we have a deeper look at the router.php in the next section.
+
It is important that you think about the sequence of the URL parameter in this stage. This will be more clear when we have a deeper look at the <tt>router.php</tt> in the next section.
  
 
The building process of JRouter is divided into two steps:
 
The building process of JRouter is divided into two steps:
Line 128: Line 133:
  
 
<source lang="php">
 
<source lang="php">
function [componentname]BuildRoute( &$query )
+
function [componentname]BuildRoute(&$query)
 
{
 
{
 
       $segments = array();
 
       $segments = array();
       if(isset($query['view']))
+
       if (isset($query['view']))
 
       {
 
       {
 
                 $segments[] = $query['view'];
 
                 $segments[] = $query['view'];
                 unset( $query['view'] );
+
                 unset($query['view']);
 
       }
 
       }
       if(isset($query['id']))
+
       if (isset($query['id']))
 
       {
 
       {
 
                 $segments[] = $query['id'];
 
                 $segments[] = $query['id'];
                 unset( $query['id'] );
+
                 unset($query['id']);
 
       };
 
       };
 
       return $segments;
 
       return $segments;
Line 146: Line 151:
 
</source>
 
</source>
  
<code>JRouter</code> passes a $query array to the <code>[''componentname'']BuildRoute</code> function. This function will add the relevant parts of the array to the $segments array in the right order and will return the properly ordered array. The content of the <code>$query</code> array needs to be unset, otherwise <code>JRouter</code> will add it to the URL in the form of a query string (i.e. any variables that are not handled by the router will be passed in the query string).
+
<code>JRouter</code> passes a <code>$query</code> array to the <code>[''componentname'']BuildRoute</code> function. This function will add the relevant parts of the array to the $segments array in the right order and will return the properly ordered array. The content of the <code>$query</code> array needs to be unset, otherwise <code>JRouter</code> will add it to the URL in the form of a query string (i.e. any variables that are not handled by the router will be passed in the query string).
  
 
The prefix ''componentname'' is the name for your component, as found in the directory holding the component's files. For instance, a component "Magic" in directory <tt>/components/com_magic/...</tt> would use a prefix <code>magic</code> (all lower case).
 
The prefix ''componentname'' is the name for your component, as found in the directory holding the component's files. For instance, a component "Magic" in directory <tt>/components/com_magic/...</tt> would use a prefix <code>magic</code> (all lower case).
Line 153: Line 158:
  
 
<source lang="php">
 
<source lang="php">
function [componentname]ParseRoute( $segments )
+
function [componentname]ParseRoute($segments)
 
{
 
{
 
       $vars = array();
 
       $vars = array();
Line 163: Line 168:
 
               case 'category':
 
               case 'category':
 
                       $vars['view'] = 'category';
 
                       $vars['view'] = 'category';
                       $id = explode( ':', $segments[1] );
+
                       $id = explode(':', $segments[1]);
 
                       $vars['id'] = (int) $id[0];
 
                       $vars['id'] = (int) $id[0];
 
                       break;
 
                       break;
 
               case 'article':
 
               case 'article':
 
                       $vars['view'] = 'article';
 
                       $vars['view'] = 'article';
                       $id = explode( ':', $segments[1] );
+
                       $id = explode(':', $segments[1]);
 
                       $vars['id'] = (int) $id[0];
 
                       $vars['id'] = (int) $id[0];
 
                       break;
 
                       break;
Line 178: Line 183:
 
What happens here? In the function <code>[''componentname'']BuildRoute</code> we arranged the items in the <code>$query</code> array in a specific sequence. This means that in this example the view is first and the id is second in the array.
 
What happens here? In the function <code>[''componentname'']BuildRoute</code> we arranged the items in the <code>$query</code> array in a specific sequence. This means that in this example the view is first and the id is second in the array.
  
By reading <code>$segments[0]</code>, we access the name of the view. We set the right view and/or identifier depending on its value and we return the <code>$vars</code> array to <code>JRouter</code>. $vars should be an associative array similar to the array that was passed to the BuildRoute method.
+
By reading <code>$segments[0]</code>, we access the name of the view. We set the right view and/or identifier depending on its value and we return the <code>$vars</code> array to <code>JRouter</code>. <code>$vars</code> should be an associative array similar to the array that was passed to the BuildRoute method.
  
The above example of the <tt>router.php</tt> is a very simple way to generate sef URL's but should show how this works quite clearly.
+
The above example of the <tt>router.php</tt> is a very simple way to generate SEF URLs but should show how this works quite clearly.
  
 
The generated URL in this example contains the name of the view and doesn't reflect the content hierarchy:
 
The generated URL in this example contains the name of the view and doesn't reflect the content hierarchy:
Line 201: Line 206:
  
 
<source lang="php">
 
<source lang="php">
JRoute::_( 'index.php?view=article&catid='.$row-­>catslug .'&id='.$row-­>slug );
+
JRoute::_('index.php?view=article&catid=' . $row-­>catslug . '&id='.$row-­>slug);
 
</source>
 
</source>
  
Line 207: Line 212:
  
 
<source lang="php">
 
<source lang="php">
JRoute::_( 'index.php?view=category&id='.$row->catslug );
+
JRoute::_('index.php?view=category&id=' . $row->catslug);
 
</source>
 
</source>
  
Line 214: Line 219:
 
<source lang="php">
 
<source lang="php">
  
function [''Componentname'']BuildRoute(&$query)
+
function [componentname]BuildRoute(&$query)
 
{
 
{
 
       $segments = array();
 
       $segments = array();
       if(isset( $query['catid'] ))
+
       if (isset($query['catid']))
 
       {
 
       {
 
                 $segments[] = $query['catid'];
 
                 $segments[] = $query['catid'];
                 unset( $query['catid'] );
+
                 unset($query['catid']);
 
       };
 
       };
       if( isset($query['id']) )
+
       if (isset($query['id']))
 
       {
 
       {
 
                 $segments[] = $query['id'];
 
                 $segments[] = $query['id'];
                 unset( $query['id'] );
+
                 unset($query['id']);
 
       };
 
       };
       unset( $query['view'] );
+
       unset($query['view']);
 
       return $segments;
 
       return $segments;
 
}
 
}
Line 235: Line 240:
  
 
<source lang="php">
 
<source lang="php">
function [''Componentname'']ParseRoute($segments)
+
function [componentname]ParseRoute($segments)
 
{
 
{
 
       $vars = array();
 
       $vars = array();
       $app =& JFactory::getApplication();
+
       $app = JFactory::getApplication();
       $menu =& $app->getMenu();
+
       $menu = $app->getMenu();
       $item =& $menu->getActive();
+
       $item = $menu->getActive();
 
       // Count segments
 
       // Count segments
       $count = count( $segments );
+
       $count = count($segments);
       //Handle View and Identifier
+
       // Handle View and Identifier
       switch( $item->query['view'] )
+
       switch ($item->query['view'])
 
       {
 
       {
 
               case 'categories':
 
               case 'categories':
                       if($count == 1) {
+
                       if ($count == 1)
 +
                      {
 
                               $vars['view'] = 'category';
 
                               $vars['view'] = 'category';
 
                       }
 
                       }
                       if($count == 2) {
+
                       if ($count == 2)
 +
                      {
 
                               $vars['view'] = 'article';
 
                               $vars['view'] = 'article';
 
                       }
 
                       }
                       $id = explode( ':', $segments[$count-1] );
+
                       $id = explode(':', $segments[$count-1]);
 
                       $vars['id'] = (int) $id[0];
 
                       $vars['id'] = (int) $id[0];
 
                       break;
 
                       break;
 
               case 'category':
 
               case 'category':
                       $id   = explode( ':', $segments[$count-1] );
+
                       $id = explode(':', $segments[$count-1]);
                       $vars['id']   = (int) $id[0];
+
                       $vars['id'] = (int) $id[0];
 
                       $vars['view'] = 'article';
 
                       $vars['view'] = 'article';
 
                       break;
 
                       break;
Line 277: Line 284:
  
 
<source lang="php">
 
<source lang="php">
$count = count( $segments );
+
$count = count($segments);
 
</source>
 
</source>
  

Latest revision as of 17:22, 28 August 2014

<translate> Search engine friendly (SEF), human-readable or clean URLs are URLs that make sense to both humans and search engines because they explain the path to the particular page they point to. Since version 1.5, Joomla! is capable of creating and parsing URLs in any format, including SEF URLs. This does not depend on URL rewriting executed by the web server, so it works even if Joomla! runs a server other than Apache with the mod_rewrite module. The SEF URLs follow a certain fixed pattern, but the user can define a short descriptive text (alias) for each segment of the URL.

Internally, the local part of a SEF URL (the part after the domain name) is called a route. Creating and processing SEF URLs is therefore referred to as routing, and the relevant code is called a router. </translate>

In Joomla!, each component is responsible for handling its own SEF URLs. Therefore, as the developer of a component, you will have to create your own router to allow your component to use SEF URLs.

Contents

The Concept

Assuming you are following standard development practices, your component is probably using "system URLs" that look a lot like http://www.example.com/index.php?option=com_yourcomponent&view=article&id=1&catid=20&Itemid=50, and your goal is to transform this into http://www.example.com/example-menu-item/20/1. As the developer, you have two tasks: signalling the system that certain pieces of text are URLs and need to be transformed, and explaining the system how to transform URLs.

Applying JRoute::_

It is difficult and inefficient for Joomla! to figure out which parts of your component's output are URLs. To support SEF URLs, you will need to change URL-generating code so that it applies JRoute::_ before outputting the URL:

echo JRoute::_('index.php?view=article&id=1&catid=20');

Notice that it is possible to leave out the parameters option and Itemid. option defaults to the name of the component currently being executed, and Itemid defaults to the current menu item's ID.

In general, you should only apply this to URLs that users and/or search engines are able to see. For example, there is no need to transform URLs used in redirects that immediately result in other redirects.

If the user turns off SEF URLs in the site's settings, JRoute::_ will produce working non-SEF URLs without any changes to the code.

Writing a router

You'll also need to write a router, which is a single file with two functions that convert system URLs to and from SEF URLs. This file needs to be placed at /components/com_yourcomponent/router.php.

The first function, [componentname]BuildRoute(&$query), must transform an array of URL parameters into an array of segments that will form the SEF URL. Schematically, the transformation works as follows:

http://www.example.com/index.php?option=com_yourcomponent&view=article&id=1&catid=20&Itemid=50
JRoute::_, called by your component or any other extension
$query = array('view' => 'article', 'id' => 1, 'catid' => 20)
Your router's com_yourcomponentBuildRoute
$segments = array(20, 1);
Joomla's internal route building (for display)
http://www.example.com/example-menu-item/20/1

The second function, [componentname]ParseRoute($segments), must transform an array of segments back into an array of URL parameters. Schematically:

http://www.example.com/example-menu-item/20/1
Joomla's internal route parsing
$segments = array(20, 1);
Your router's com_yourcomponentParseRoute
$query = array('view' => 'article', 'id' => 1, 'catid' => 20)

The two functions must cooperate in such a way that the original URL can be reconstructed. You can think of BuildRoute as a form of encoding and ParseRoute as the corresponding decoding. When the original URL isn't properly reproduced, your component will stop working.

Preparing Your Data for Routing

Clearly, any URL format needs to contain some kind of information that identifies the data you want to show. If your system URLs look like http://www.example.com/index.php?option=com_yourcomponent&view=article&id=1&catid=20&Itemid=50, that information is currently the id URL parameter (id=1). You probably want your SEF URLs to contain a textual description of the data they point to. In Joomla!, this is usually done by giving your users a way to enter an alias to be used in the URL.

The Alias

Even if your users can enter an alias, they might not do so, leaving the generation of a sensible alias up to your component. If your data has a title field, you can use that as a candidate for the alias (like the core Content component does).

Considering that the alias will be used in URLs, it has to be URL safe. Joomla! provides a method making arbitrary strings URI safe, which includes replacing accented UTF­8 characters by their ASCII­7 equivalents, white spaces by hyphens, etc. Whether the user entered the alias or a candidate has been chosen automatically, you should ensure that the above requirements for a URL safe alias are met. A good place for implementing this, if you are using JTable, is the JTable::check() method, which is called during the save process. Have a look at this example code:

function check()
{
    jimport('joomla.filter.output');
    if (empty($this->alias))
    {
            $this->alias = $this->title;
    }
    $this->alias = JFilterOutput::stringURLSafe($this->alias);
 
    /* All your other checks */
    return true;
}

If the alias field is empty the title is used as alias. Then the alias is made URL safe using the JFilterOutput::stringURLSafe() method.

The Slug

A slug is used to minimise the amount of code you need to support SEF URLs. It consists of the numerical identifier (id) your system URLs used, a colon (:), and the alias you created as described above.

Consider a SEF URL for an Article with id 1 and title "Welcome to Joomla!". The automatically generated alias for this article is welcome-to-joomla, and the slug becomes 1­:welcome­-to­-joomla. In the Content component, the two elements are combined during the database query in the model (a represents #__content):

$query = 'SELECT a.*, '.
         'CASE WHEN CHAR_LENGTH(a.alias) THEN CONCAT_WS(":", a.id, a.alias) ELSE a.id END as slug,'
         /*...*/;

The advantage of this method of creating a slug is that you can simply use the slug as a drop-in replacement for the id in most places. For example, you don't need to check for and remove the colon and the alias from the request data manually: if you use JInput's int (integer) filter, it will do that automatically.

Routing URLs

The JRoute::_ method translates the internal Joomla! URL to a custom URL. JRoute::_ has three parameters and its prototype is:

JRoute::_($url, $xhtml = true, $ssl = null);

Where:

  • $url is a string containing the absolute or relative internal Joomla! URL.
  • $xhtml is a boolean value that specifies whether or not the output should be in XHTML. This parameter is optional and if omitted defaults to true.
  • $ssl is an integer value that specifies whether the URI should be secure. It should be set to 1 to force the URI to be secure using the global secure site URI, 0 to leave it in the same state as when it was passed, and -1 to force the URI to be unsecure using the global unsecure site URI.

The most important parameter is $url. A call to this method might look like:

JRoute::_('index.php?view=article&id=' . $row->slug);

$row-­>slug is the value that was generated in step 2 from a combination of id and title alias.

Another advantage of using JRoute::_ is that the router now handles $option (the component name) and the $Itemid (the menu item ID). The component itself doesn't have to know its name ($option) or the active menu item ($Itemid) like it did in previous version of Joomla!.

It is important that you think about the sequence of the URL parameter in this stage. This will be more clear when we have a deeper look at the router.php in the next section.

The building process of JRouter is divided into two steps:

  • Create the application route. The application route is fully handled by JRouter and the component developer doesn’t have to do anything to make it work.
  • Create the component route. To create the component route, JRouter looks for the router.php in the component directory which is responsible for building the route for the component.

The Component Router

We will have two functions in the router.php. One is responsible for building the URL and the other is responsible for parsing it. In the next examples, a very basic and a more advanced one, we assume that we have three views that links can point to. The first is a categories overview (view=categories), the second is a single category (view=category) and the third is a single article (view=article).

The file router.php should be in the site area of your component. It is not used on admin/backend pages. Don't forget to add it to your XML manifest file in the site folder.

A Simple Example

This simple example will illustrate the basics of implementing a router for your component.

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;
}

JRouter passes a $query array to the [componentname]BuildRoute function. This function will add the relevant parts of the array to the $segments array in the right order and will return the properly ordered array. The content of the $query array needs to be unset, otherwise JRouter will add it to the URL in the form of a query string (i.e. any variables that are not handled by the router will be passed in the query string).

The prefix componentname is the name for your component, as found in the directory holding the component's files. For instance, a component "Magic" in directory /components/com_magic/... would use a prefix magic (all lower case).

The next function in the router.php parses the URL:

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;
}

What happens here? In the function [componentname]BuildRoute we arranged the items in the $query array in a specific sequence. This means that in this example the view is first and the id is second in the array.

By reading $segments[0], we access the name of the view. We set the right view and/or identifier depending on its value and we return the $vars array to JRouter. $vars should be an associative array similar to the array that was passed to the BuildRoute method.

The above example of the router.php is a very simple way to generate SEF URLs but should show how this works quite clearly.

The generated URL in this example contains the name of the view and doesn't reflect the content hierarchy:

http://www.example.com/[menualias]/[view]/[slug]

A More Advanced Example

In the next example we will try to get rid of the need for the view and we will try to reflect the current hierarchy level in the URL.

The goal is URL's that look like:

  • When viewing an article: http://www.example.com/[menualias]/[category]/[article]
  • When viewing a category: http://www.example.com/[menualias]/[category]
  • When viewing the categories overview: http://www.example.com/[menualias]

Let's assume we have done step 1 and 2 also for the category.

The link to the article would look like this:

JRoute::_('index.php?view=article&catid=' . $row-­>catslug . '&id='.$row-­>slug);

And the Link to the category would look like this:

JRoute::_('index.php?view=category&id=' . $row->catslug);

The corresponding router.php:

function [componentname]BuildRoute(&$query)
{
       $segments = array();
       if (isset($query['catid']))
       {
                $segments[] = $query['catid'];
                unset($query['catid']);
       };
       if (isset($query['id']))
       {
                $segments[] = $query['id'];
                unset($query['id']);
       };
       unset($query['view']);
       return $segments;
}

The difference now is that we don’t add the name of the view to the $segments array. We still unset the view key since otherwise, JRouter would add it to the URL as part of the query string. Another new thing here is the additional parameter catid that we push into the $segments array.

function [componentname]ParseRoute($segments)
{
       $vars = array();
       $app = JFactory::getApplication();
       $menu = $app->getMenu();
       $item = $menu->getActive();
       // Count segments
       $count = count($segments);
       // Handle View and Identifier
       switch ($item->query['view'])
       {
               case 'categories':
                       if ($count == 1)
                       {
                               $vars['view'] = 'category';
                       }
                       if ($count == 2)
                       {
                               $vars['view'] = 'article';
                       }
                       $id = explode(':', $segments[$count-1]);
                       $vars['id'] = (int) $id[0];
                       break;
               case 'category':
                       $id = explode(':', $segments[$count-1]);
                       $vars['id'] = (int) $id[0];
                       $vars['view'] = 'article';
                       break;
       }
       return $vars;
}

You can see that this ParseRoute function has a lot of different code parts in comparison to the previous. The reason for this is simple. We don’t have the name of the view in the $segments array and we need to find another way to determine it.

We need to find out which level of hierarchy we are in by receiving the root element. We do this by looking to the view name of the active menu item:

$item-­>query['view']

Also we need to know the number of items in the $segments array:

$count = count($segments);

With this information we can correctly set the view for all possible three cases:

  • The menu item is a link to the categories view and the $segments array has two items ($catid and $id). In this case we know that we need to parse a link to an article.
  • The menu item is a link to the categories view and the $segments array has one item ($id). In this case we know that we need to parse a link to a category.
  • The menu item is a link to a category. In this case, we know that any item in the $segments array is the identifier for an article.

The result of all this code is clean and human-readable component URLs.

Routers and Menu Items

A last important part of creating a router is considering what to do with menu items. As explained on Search Engine Friendly URLs, the output of the component router is used after the first segment of a route, the first segment being the menu item's alias. This creates a difficult question: how is your router and/or other code to know which menu item to route through?

Suppose, for example, that your component is currently producing output for the page /dogs, which lists all dogs in the system. Of course, the items in the list need to be links to pages that display more details about one dog. What should the URL to the dog with ID 21 and name Fido become? Using a router that works according to the principles we've seen so far, the route that is produced is dogs/21-fido, or with some additional work /dogs/fido. But perhaps the user has created a menu item with the alias mydoggy which displays exactly that dog's details. Then it is probably the user's intention to route this URL through that menu item, and the item in the list should link to the page /mydoggy.

More generally, whenever you are building a route, you will need to find the menu item that is most suitable as a starting point for building your route. The term starting point is emphasized because the rest of the route depends on the configuration of the menu item. In our example above, /dogs/21-fido is an acceptable route, /mydoggy is arguably even better, but /mydoggy/21-fido is simply wrong, since /mydoggy is in itself a menu item that is set up to display fido's information.

Several approaches are available to tackle this problem. Joomla!'s core components take a mixed approach, separating responsibilities in two units of code: the router itself and the so-called [componentname]RouteHelper. The [componentname]RouteHelper provides methods that find the most suitable menu item for a given piece of data to be displayed, while the router analyzes the menu item and puts any information that is not determined by the menu item's configuration into the route. This does mean that the calling code must explicitly call the helper's method before routing (echo JRoute::_(DogsRouteHelper::getDogRoute(21));).

See Also

There is a useful thread on this subject here: jtopic:148632 (note, may be out of date)

For details on the internals of routing, see Routing implementation details.