main | downloads | documentation | comunity | about lionframework

Url Routing Engine

Routing user requests

Table of Contents

Introduction

This is the Lion native URL rewrite engine, delegating the URL parsing from the webserver to Lion itself.

URL rewriting is a method of creating search engine friendly URLs, also known as SEO (Search Engine Optimization) URLs. Dynamic URLs have apparently a negative effect on search engine ranking. To get around this issue the URL rewriting technique is used. URL rewrite tools can examine a website and produce a simplified link for each resource on the website.

i.e. the link:

http://yourdomain.com/index.php?module=invoices&view_id=10948

can be changed to

http://yourdomain.com/invoices/10948.html

As part of a web application usability regarding the URLs, general recommendation is that URLs be chosen so that they:

  • Are short.
  • Are easy to type.
  • Visualize the site structure.
  • Are guessable, allowing the user to navigate through the site by using intuition to fid resourcesoff parts of the URL.

Routes

Different URL formats accepted by Lion are specified within route definitions. A route is a way to describe the mapping between an URL and how to route the execution flow.
A route defines a pattern for the URL, in pure perl compatible regular expression syntax. First found route matching an URL will be the one used to process the request.

A route defines the following components:

  • Which front controller will attend the request.
  • Which action/controller will be executed.
  • Which other parameters will be appended to the request.

Defining a Route

The way to define routes is by adding them to the configuration (by default, the config/routes.xml file)

i.e.

  1. <?xml version "1.0" standalone="yes"?>
  2. <configuration>
  3.  
  4.   <routes>
  5.  
  6.     <route id="invoice_search" uri-pattern="\/invoices\/$invoice_id\.html$">
  7.  
  8.       <front-controller class="__HttpFrontController"/>
  9.  
  10.       <action controller="invoices" code="search"/>
  11.  
  12.       <parameter name="invoice_id" value="$invoice_id"/>
  13.  
  14.       <variable name="$invoice_id" var-pattern="^\d+$"/>
  15.  
  16.     </route>
  17.  
  18. </configuration>

In this example we are defining a route for URLs matching the regular expression \/invoices\/$invoice_id\.html$. It is, URLs like /invoices/xxx.html, being xxx a number.
An URL matching this pattern will be routed as following:

  • The front controller class to attend the request will be the __HttpFrontController class
  • The controller and action to be executed will be the default action within the invoices controller
  • A parameter will be appended to the request, being the value of the variable $invoice_id, it is, the number xxx

Note that the way to define variables within the URL regular expression is by ussing the prefix dollar ($) followed by an identifier. In our example, $invoice_id.
It's also important to note that we can also impose a regular expression to restrict the value for a variable. In our example, we are restricting the value for $invoice_id to the pattern ^\d+$.

According to that, the full regular expression that need to matches an URL in order to be routed by the invoice_search route is the following one: \/invoices\/\d+\.html$
i.e. http://yourdomain.com/invoices/10948.html

Now, let's see a more generic example:

  1. <?xml version "1.0" standalone="yes"?>
  2. <configuration>
  3.  
  4.   <routes>
  5.  
  6.     <route id="search" uri-pattern="\/$module\/$search_id\.html$">
  7.  
  8.       <front-controller class="__HttpFrontController"/>
  9.  
  10.       <action controller="$module" code="search"/>
  11.  
  12.       <parameter name="search_id" value="$search_id"/>
  13.  
  14.       <variable name="$module" var-pattern="^(invoices|clients|accounts)$"/>
  15.  
  16.       <variable name="$search_id" var-pattern="^\d+$"/>
  17.  
  18.     </route>
  19.  
  20. </configuration>

In this example, we are reusing the same route definition for the controllers invoices, clients and accounts.

Conditional parameters

Lion allows the use of conditional parameters, meaning parameters that will be added just if a condition is satisfaced.
Conditions can be defined in 2 different ways:

  • By checking if a variable has a concrete value, by ussing the if-equals tag.
  • By checking if a variable has been set, by ussing the if-isset tag.

i.e., checking the variable value:

  1. <?xml version "1.0" standalone="yes"?>
  2. <configuration>
  3.  
  4.   <routes>
  5.  
  6.     <route id="search" uri-pattern="\/$module\/$search_id\.$extension$">
  7.  
  8.       <front-controller class="__HttpFrontController"/>
  9.  
  10.       <action controller="$module" code="search"/>
  11.  
  12.       <if-equals variable="$extension" value="html">
  13.         <parameter name="use_cache" value="1"/>
  14.       </if-equals>
  15.  
  16.       <parameter name="search_id" value="$search_id"/>
  17.  
  18.       <variable name="$module" var-pattern="^(invoices|clients|accounts)$"/>
  19.  
  20.       <variable name="$search_id" var-pattern="^\d+$"/>
  21.  
  22.       <variable name="$extension" var-pattern="^(html|action)$"/>
  23.  
  24.     </route>
  25.  
  26. </configuration>

In this example, we are adding a variable (use_cache) just in case the extension is 'html'.

i.e., checking if a variable has been set:

  1. <?xml version "1.0" standalone="yes"?>
  2. <configuration>
  3.  
  4.   <routes>
  5.  
  6.     <route id="search" uri-pattern="\/$module\/$search_id(\_$page_id)?\.html$">
  7.  
  8.       <front-controller class="__HttpFrontController"/>
  9.  
  10.       <action controller="$module" code="search"/>
  11.  
  12.       <parameter name="search_id" value="$search_id"/>
  13.  
  14.       <if-isset variable="$page_id">
  15.         <parameter name="pagination" value="1"/>
  16.         <parameter name="page_id" value="$page_id"/>
  17.       </if-isset>
  18.  
  19.       <variable name="$module" var-pattern="^(invoices|clients|accounts)$"/>
  20.  
  21.       <variable name="$page_id" var-pattern="^\d+$"/>
  22.  
  23.       <variable name="$search_id" var-pattern="^\d+$"/>
  24.  
  25.     </route>
  26.  
  27. </configuration>

In this example, we are adding 2 variables (pagination and page_id) just if the page_id is set.


Default routes

Lion defines a set of routes that will be availabled by default, and defined in the /config/default/routes.xml file, located in the lion directory:

  • Route default: Defined as <controller>[.<action>].(action|lion). This route uses the __HttpFrontController and has been defined in order to route the most common client requests. The only thing to highlight here is the fact that by ussing the suffix .lion we'll switch to lion administrative area context. Otherwise, the application context will be used.
  • Route ajax: Defined as index.ajax. This route uses the __AjaxFrontController and has been defined in order to route the ajax requests.
  • Route resource: Defined as /resources/<resource_file>. This route uses the __ResourceFrontController and has been defined in order to route request for images, css and .js files protected by lion (i.e. a js for a concrete UI component).
  • Route unknow_url_format: This is the last route to be evaluated by lion. It accepts any URL format, and redirects to the error page with the error code 55701 (Unknwo URL format). If an URL does not match any other route, will be handled by this one.

The __Uri class

By delegating the URL rewrite to Lion, the URL format depends on the route definitions. Because URL formats are sensible to be changed, is a bad practice to hardcode URLs within the code/templates since they could stop working after changing the URL format within the route specification.

The __Uri class has been designed to cover this aspect. It provides methods to discompound an URL into components as well as to do the inverse task, to compose an URL as from the components.

i.e. Discompound an URL into components:

  1. <?php
  2.  
  3. //Discompound an url into single elements:
  4.                      createUri('http://yourdomain.com/invoices/10948.html');
  5.  
  6. $parameters $uri->getParameters()
  7. //--> [invoice_id => 10948]
  8.  
  9. $controller_code $uri->getControllerCode()
  10. //--> invoices
  11.  
  12. $action_code $uri->getActionCode()
  13. //--> default
  14.  
  15. $route_id $uri->getRouteId()
  16. //--> invoice_search
  17.  
  18. //we can also know which front controller attends the request:
  19. $front_controller_class $uri->getFrontControllerClass();
  20. //--> __HttpFrontController

i.e. Compound an URL as from components:

  1. <?php
  2.  
  3. //Compound an url from single elements:
  4. $uri2 new __Uri();
  5. $uri2->setControllerCode('invoices');
  6. $uri2->setParameters(array('invoice_id' => 10948));
  7. $uri2->setRouteId('invoice_search');
  8.  
  9. $url $uri2->getUrl()
  10. //--> /invoices/10948.html
  11.  
  12. $url $uri2->getAbsoluteUrl();
  13. //--> http://yourdomain.com/invoices/10948.html

See the __Uri definition for more information.

Reference Manual

Table of content

    Getting started Request dispatching Context container MVC Component model: Security Configuration Session handling I18n Cache Logging Error handling API reference

      Tutorials: Frequently Asqued Questions

      See also:
      Copyright (c) 2008, www.lionframework.org