Actions

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

From Joomla! Documentation

m (Updated contents)
(Update 3.0 to 3.1)
Line 1: Line 1:
{{incomplete}}
+
{{version|1.5,2.5,3.1}}
 
{{:Search Engine Friendly URLs}}
 
{{:Search Engine Friendly URLs}}
In Joomla!, each [[component]] is responsible for handling its own human-readable URLs. Therefore, as the [[Developers|developer]] of a component, you will have to create your own '''router''' to allow your component to use human-readable 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.
 +
 
 +
== The Concept ==
 +
 
 +
Assuming you are following standard development practices, your component is probably using "system URLs" that look a lot like <tt><nowiki>http://www.example.com/index.php?option=com_yourcomponent&view=article&id=1&catid=20&Itemid=50</nowiki></tt>, and your goal is to transform this into <tt><nowiki>http://www.example.com/example-menu-item/20/1</nowiki></tt>. 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 <code>JRoute::_</code> ===
 +
 
 +
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 <code>JRoute::_</code> before outputting the URL:
 +
 
 +
<source lang="php">
 +
echo JRoute::_('index.php?view=article&id=1&catid=20');
 +
</source>
 +
 
 +
Notice that it is possible to leave out the parameters <code>option</code> and <code>Itemid</code>. <code>option</code> defaults to the name of the component currently being executed, and <code>Itemid</code> 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, <code>JRoute::_</code> 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 <tt>/components/com_yourcomponent/router.php</tt>.
 +
 
 +
The first function, <code>[componentname]BuildRoute(&$query)</code>, must transform an array of URL parameters into an array of segments that will form the SEF URL. Schematically, the transformation works as follows:
 +
 
 +
:<tt><nowiki>http://www.example.com/index.php?option=com_yourcomponent&view=article&id=1&catid=20&Itemid=50</nowiki></tt>
 +
 
 +
::<span style="font-size: xx-large">&darr;</span> <code>JRoute::_</code>, called by your component or any other extension
 +
 
 +
:<code>$query = array('view' => 'article', 'id' => 1, 'catid' => 20)</code>
 +
 
 +
::<span style="font-size: xx-large">&darr;</span> Your router's <code>com_yourcomponentBuildRoute</code>
 +
 
 +
:<code>$segments = array(20, 1);</code>
 +
 
 +
::<span style="font-size: xx-large">&darr;</span> Joomla's internal route building (for display)
 +
 
 +
:<tt><nowiki>http://www.example.com/example-menu-item/20/1</nowiki></tt>
 +
 
 +
The second function, <code>[componentname]ParseRoute($segments)</code>, must transform an array of segments back into an array of URL parameters. Schematically:
 +
 
 +
:<tt><nowiki>http://www.example.com/example-menu-item/20/1</nowiki></tt>
 +
 
 +
::<span style="font-size: xx-large">&darr;</span> Joomla's internal route parsing
 +
 
 +
:<code>$segments = array(20, 1);</code>
 +
 
 +
::<span style="font-size: xx-large">&darr;</span> Your router's <code>com_yourcomponentParseRoute</code>
 +
 
 +
:<code>$query = array('view' => 'article', 'id' => 1, 'catid' => 20)</code>
 +
 
 +
The two functions must cooperate in such a way that the original URL can be reconstructed. You can think of <code>BuildRoute</code> as a form of [[wikipedia:Encoding|encoding]] and <code>ParseRoute</code> as the corresponding decoding. When the original URL isn't properly reproduced, your component will stop working.
  
 
== Preparing Your Data for Routing ==
 
== Preparing Your Data for Routing ==
Line 26: Line 78:
 
</source>
 
</source>
  
If the alias field is empty the title will be used as alias. Then the alias will be made URLSafe using the JFilterOutput::stringURLSafe() method.
+
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.
  
 
=== The Slug ===
 
=== The Slug ===
Line 40: Line 92:
 
== Routing URLs ==
 
== Routing URLs ==
  
The <code>JRoute::_</code> method translates the internal Joomla! URL to a custom URL. <code>JRoute</code> has three parameters and its prototype is:
+
The <code>JRoute::_</code> method translates the internal Joomla! URL to a custom URL. <tt>JRoute</tt> 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>
Line 63: Line 115:
  
 
* 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 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.
+
* Create the component route. To create the component route, JRouter looks for the <tt>router.php</tt> in the component directory which is responsible for building the route for the component.
  
 
== The Component Router ==
 
== 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).
+
We will have two functions in the <tt>router.php</tt>. 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 (<code>view=categories</code>), the second is a single category (<code>view=category</code>) and the third is a single article (<code>view=article</code>).
  
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 installation XML in the site folder.
+
The file <tt>router.php</tt> 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 files|manifest file]] in the site folder.
  
 
=== A Simple Example ===
 
=== A Simple Example ===
Line 96: Line 148:
 
<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 $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).
  
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 <code>/components/com_magic/...</code> 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).
  
The next function in the <code>router.php</code> parses the URL:
+
The next function in the <tt>router.php</tt> parses the URL:
  
 
<source lang="php">
 
<source lang="php">
Line 128: Line 180:
 
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>. $vars should be an associative array similar to the array that was passed to the BuildRoute method.
  
The above example of the <code>router.php</code> 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:
  
<code><nowiki>http://www.example.com/[menualias]/[view]/[slug]</nowiki></code>
+
<tt><nowiki>http://www.example.com/[menualias]/[view]/[slug]</nowiki></tt>
  
 
=== A More Advanced Example ===
 
=== A More Advanced Example ===
Line 140: Line 192:
 
The goal is URL's that look like:
 
The goal is URL's that look like:
  
* When viewing an article: <code><nowiki>http://www.example.com/[menualias]/[category]/[article]</nowiki></code>
+
* When viewing an article: <tt><nowiki>http://www.example.com/[menualias]/[category]/[article]</nowiki></tt>
* When viewing a category: <code><nowiki>http://www.example.com/[menualias]/[category]</nowiki></code>
+
* When viewing a category: <tt><nowiki>http://www.example.com/[menualias]/[category]</nowiki></tt>
* When viewing the categories overview: <code><nowiki>http://www.example.com/[menualias]</nowiki></code>
+
* When viewing the categories overview: <tt><nowiki>http://www.example.com/[menualias]</nowiki></tt>
  
 
Let's assume we have done step 1 and 2 also for the category.
 
Let's assume we have done step 1 and 2 also for the category.
Line 158: Line 210:
 
</source>
 
</source>
  
The corresponding <code>router.php</code>:
+
The corresponding <tt>router.php</tt>:
  
 
<source lang="php">
 
<source lang="php">
Line 230: Line 282:
 
With this information we can correctly set the view for all possible three cases:
 
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 <code>$segments</code> array has two items (<code>$catid</code> and <code>$id</code>). 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 <code>$segments</code> array has two items (<code>$catid</code> and <code>$id</code>). 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 the categories view and the <code>$segments</code> array has one item (<code>$id</code>). 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 menu item is a link to a category. In this case, we know that any item in the <code>$segments</code> array is the identifier for an article.
  
The result of all this code is nice and human readable component URL's.
+
The result of all this code is clean and human-readable component URLs.
  
== Application Route Parsing ==
+
== Routers and Menu Items ==
  
The [[Application execution order]] outlines that the route (URL) is parsed immediately after initialisation is complete.  Since fancy URL's are not treated (yet) in the Administrator, we will follow the route parsing process in detail when <code>JSite::route</code> in the <code>index.php</code> file.
+
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?
  
* Call to <code>JApplication::route</code>
+
Suppose, for example, that your component is currently producing output for the page <tt>/dogs</tt>, 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 <tt>dogs/21-fido</tt>, or with some additional work <tt>/dogs/fido</tt>. But perhaps the user has created a menu item with the alias <tt>mydoggy</tt> 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 <tt>/mydoggy</tt>.
** Clone the URI
+
** Call to <code>JApplication::getRouter</code>
+
*** Call to <code>JRouter::getInstance</code> passing the type ("site")
+
** Call to <code>JRouterSite::parse</code> passing the URI
+
*** Strip the suffix if applicable (added to $vars['format'])
+
*** Re-set the route (URI)
+
*** Call to <code>JRouter::parse</code> passing the URI
+
**** Call to <code>JRouterSite::_processParseRules</code> passing the URI (this will call custom route rules)
+
***** Call to <code>JRouter::_processParseRules</code> passing the URI
+
****** Call any custom routing rules (probably added via a system plugin using the <code>onAfterInitialise</code> event trigger) passing the URI
+
****** Returns an array of vars
+
***** If SEF mode, replace <code>start</code> variable with </code>limitstart</code>
+
**** If raw mode, call to <code>JRouterSite::_parseRawRoute</code> passing the URI
+
**** If SEF mode, call to <code>JRouterSite::_parseSefRoute</code> passing the URI
+
***** If the route (the URI path) is empty, load it from the default menu item; set the active menu item as the default
+
***** If first part is <code>/component/com_content</code>, set the <code>option</code> as the second segement.  Null the <code>Itemid</code>.
+
***** Else, loop through menu alias values and take off segments that match as the menu tree is traversed.  Set <code>option</code> and <code>Itemid</code> based on the last menu item found.
+
***** If the <code>Itemid</code> is set in the URL, set the active menu item based on this value.
+
***** Push the vars collected so far (eg, <code>option</code>, <code>Itemid</code>, etc) into the router object (<code>$this</code>).
+
***** If the route and <code>option</code> is set, load the component router;
+
***** Else, get the active menu item and get the route vars from it
+
  
 +
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, <tt>/dogs/21-fido</tt> is an acceptable route, <tt>/mydoggy</tt> is arguably even better, but <tt>/mydoggy/21-fido</tt> is simply wrong, since <tt>/mydoggy</tt> is in itself a menu item that is set up to display <tt>fido</tt>'s information.
  
== Application Route Building ==
+
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 <code>[componentname]RouteHelper</code>. The <code>[componentname]RouteHelper</code> 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 (<code>echo JRoute::_(DogsRouteHelper::getDogRoute(21));</code>).
  
TODO
+
== See Also ==
  
== Custom Router Rules ==
+
There is a useful thread on this subject here: [[jtopic:148632]] (note, may be out of date)
  
TODO
+
For details on the internals of routing, see [[Routing implementation details]].
 
+
== Additional References ==
+
 
+
There is a useful thread on this subject here: [[jtopic:148632]] (note, may be out of date)
+
 
<noinclude>[[Category:Tutorials]][[Category:Component Development]][[Category:Search Engine Friendly URLs]]</noinclude>
 
<noinclude>[[Category:Tutorials]][[Category:Component Development]][[Category:Search Engine Friendly URLs]]</noinclude>

Revision as of 03:40, 29 April 2013