Actions

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

From Joomla! Documentation

m (Fixed mistake)
m (Improved naming consistency)
Line 1: Line 1:
 
{{incomplete}}
 
{{incomplete}}
 
{{: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 ==
 
== 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>. You'll need to write a router, which is a single file (<tt>/components/com_yourcomponent/router.php</tt>) with two functions that transform system URLs back and forth to human-readable URLs.
+
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>. You'll 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 URL. Schematically, the transformation works as follows:
+
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>
 
:<tt><nowiki>http://www.example.com/index.php?option=com_yourcomponent&view=article&id=1&catid=20&Itemid=50</nowiki></tt>
Line 264: Line 264:
 
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 $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 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 nice and human readable component URL's.
+
The result of all this code is clean and human-readable component URLs.
  
 
== Application Route Parsing ==
 
== Application Route Parsing ==

Revision as of 08:28, 22 August 2012