Difference between revisions of "Search Engine Friendly URLs"

From Joomla! Documentation

m (fixed a misspelt filename)
(Added link to http://docs.joomla.org/Routing)
Line 33: Line 33:
  
 
Joomla allows you to create your own routing mechanism.  In order to create this mechanism you must have a plugin that overrides the JPlugin::onAfterInitialise() function.  This function then parses the URL and creates the needed variables in $_GET before the standard Joomla routing is done.
 
Joomla allows you to create your own routing mechanism.  In order to create this mechanism you must have a plugin that overrides the JPlugin::onAfterInitialise() function.  This function then parses the URL and creates the needed variables in $_GET before the standard Joomla routing is done.
 +
 +
==See Also==
 +
http://docs.joomla.org/Routing

Revision as of 16:21, 2 December 2009

Joomla Routes

Joomla routes are created by the JRouter class. This class looks in the component root of the component specified in the "option" portion of the query string and includes the router.php file in that component's root directory. It then calls one of two functions: one for creating the SEF link and one for interpreting the SEF link.

The JRouter class is overridden by the Joomla CMS in /includes/router.php. In this file the build and parse functions are overridden to properly build and parse the URLs for the Joomla CMS.

com_component/router.php

The router.php file should contain the following two functions:

  • ContentBuildRoute - this builds the SEF url
    • Parameters
      • $query - this is a named array containing the querystring variables
    • Returns: an array of segments where each segment is separated by a '/' when later combined to create the actual link (the items in the array should not contain '/' characters)
  • ContentParseRoute - this interprets an SEF url
    • Parameters
      • $segments - this is an array that contains the segments of the url requested.
    • Returns: a name => value array of the querystring variables that the link maps to

The SEF Plugin

The Joomla System SEF plugin inherits JPlugin and overrides the onAfterRender() function. In this function the body of the response that will be sent to the browser is retrieved using JResponse::getBody(). The body of the response is then searched for links containing "/index.php..." and replaces them with a correct SEF url by calling JRoute::_(url).

JRoute builds SEF URLs by instantiating a JRouter object and requesting that it build the correct link from the passed in URL.


Handling SEF URLs

By default the SEF URLs are handled by the the JRouterSite object (from /includes/router.php) and is called by a call to JApplication::route() in index.php. This call is made on the $mainframe variable which is actually an instance of JSite (from /includes/application.php).

JApplication::route() has a non-destructive result on the $_GET array. By this I mean that JApplication::route() sets variables in $_GET by calling JRequest::set() with the overwrite flag set to false. Thus if a variable name is returned from JRouter::route() that is already in $_GET then it will not put that value into $_GET. This allows for custom routing.

Custom Routing

Joomla allows you to create your own routing mechanism. In order to create this mechanism you must have a plugin that overrides the JPlugin::onAfterInitialise() function. This function then parses the URL and creates the needed variables in $_GET before the standard Joomla routing is done.

See Also[edit]

http://docs.joomla.org/Routing