Class AkRouter

(line 52)

Description

AkObject
   |
   --AkRouter

Located in File: /AkRouter.php

Native PHP URL rewriting for the Akelos Framework

This class implements PHP based URL rewriting for the Akelos Framework, thus shifting the responsibility of URL parsing from the webserver to the Akelos Framework itself. This has been a requested feature for two primary reasons.

  • Not all webservers support rewriting. By moving this code to the core, the framework is able to function out of the box on almost all webservers.
  • A rewriting implementation in the Akelos Framework can also be used to generate custom URLs by linking it to the standard URL helpers such as url_for, link_to, and redirect_to.



Class Variables

Summary:

Class Constants

Summary:

Method Detail

Summary:
AkRouter __construct ()
void connect (string $url_pattern, [array $options = array()], [array $requirements = null])
array getRoutes ()
void map ( $url_pattern, [ $options = array()], [ $requirements = null])
mixed toParams (string $url)
string toUrl ([array $params = array()])
void _urlDecode ( $input)
void _urlEncode ( $input)

Constructor __construct (line 65)

AkRouter __construct( )

Overrides : AkObject::__construct() Class constructor, overriden in descendant classes

Info

Method connect (line 478)

void connect( string $url_pattern, [array $options = array()], [array $requirements = null])

Add a rewrite rule

Rewrite rules are defined on the file

  1. config/routes.php

Rules that are defined first take precedence over the rest.

Parameters

  • string $url_pattern:

    URL patterns have the following format:

    • /static_text
    • /:variable (will load $variable)
    • /*array (will load $array as an array)

  • array $options:

    Options is an array with and array pair of field=>value The following example

    1. array('controller' => 'page')
    sets var 'controler' to 'page' if no 'controller' is specified in the $url_pattern param this value will be used.

    The following constants can be used as values:

    1.  OPTIONAL // 'var_name'=> OPTIONAL, will set 'var_name' as an option
    2.  COMPULSORY // 'var_name'=> COMPULSORY, will require 'var_name' to be set

  • array $requirements:

    $requirements holds an array with and array pair of field=>value where value is a perl compatible regular expression that will be used to validate rewrite rules The following example

    1. array('id'=>'/\d+/')
    will require that var 'id' must be a numeric field.

    NOTE:If option 'id'=>OPTIONAL this requirement will be used in case 'id' is set to something

Info

  • access - public

Method getRoutes (line 85)

array getRoutes( )

$this->_loaded_routes getter

Use this method to get $this->_loaded_routes value

Info

  • return - Returns Loaded Routes array.
  • access - public

Method map (line 601)

void map( $url_pattern, [ $options = array()], [ $requirements = null])

Alias for map

Parameters

  • $url_pattern:
  • $options:
  • $requirements:

Info

Method toParams (line 387)

mixed toParams( string $url)

Gets the parameters from a Akelos Framework friendly URL.

This method returns the parameters found in an Akelos Framework friendly URL.

This function will inspect the rewrite rules and will return the params that match the first one.

Parameters

  • string $url: URL to get params from.

Info

  • return -

    Having the following rewrite rules:

    1.  $Router =new AkRouter();
    2.  
    3.  $Router->map('/setup/*config_settings',array('controller'=>'setup'));
    4.  $Router->map('/customize/*options/:action',array('controller'=>'themes','options'=>3));
    5.  $Router->map('/blog/:action/:id',array('controller'=>'post','action'=>'list','id'=>OPTIONAL),array('id'=>'/\d{1,}/'));
    6.  $Router->map('/:year/:month/:day'array('controller' => 'articles','action' => 'view_headlines','year' => COMPULSORY,'month' => 'all','day' => OPTIONALarray('year'=>'/(20){1}\d{2}/','month'=>'/((1)?\d{1,2}){2}/','day'=>'/(([1-3])?\d{1,2}){2}/'));
    7.  $Router->map('/:webpage'array('controller' => 'page''action' => 'view_page''webpage' => 'index'),array('webpage'=>'/(\w|_)+/'));
    8.  $Router->map('/'array('controller' => 'page''action' => 'view_page''webpage'=>'index'));
    9.  $Router->map('/:controller/:action/:id');

    We get the following results:

    1. $Router->toParams('/contact_us');
    Produces: array('controller'=>'page','action'=>'view_page','webpage'=>'contact_us');

    1. $Router->toParams('/');
    Produces: array('controller'=>'page','action'=>'view_page','webpage'=>'index');

    1. $Router->toParams('');
    Produces: array('controller'=>'page','action'=>'view_page','webpage'=>'index');

    1. $Router->toParams('/blog/');
    Produces: array('controller'=>'post','action'=>'list','id'=>null);

    1. $Router->toParams('/blog/view');
    Produces: array('controller'=>'post','action'=>'view','id'=>null);

    1. $Router->toParams('/blog/view/10/');
    Produces: array('controller'=>'post','action'=>'view','id'=>'10');

    1. $Router->toParams('/blog/view/newest/');
    Produces: array('controller'=>'blog','action'=>'view','id'=>'newest');

    1. $Router->toParams('/2005/10/');
    Produces: array('controller' => 'articles','action' => 'view_headlines','year' => '2005','month' => '10', 'day' => null);

    1. $Router->toParams('/2006/');
    Produces: array('controller' => 'articles','action' => 'view_headlines','year' => '2006','month' => 'all', 'day' => null);

    1. $Router->toParams('/user/list/12');
    Produces: array('controller' => 'user','action' => 'list','id' => '12');

    1. $Router->toParams('/setup/themes/clone/12/');
    Produces: array('controller' => 'setup','config_settings' => array('themes','clone','12'));

    1. $Router->toParams('/customize/blue/css/sans_serif/clone/');
    Produces: array('controller' => 'themes','options' => array('blue','css','sans_serif'), 'action'=>'clone');

    This function returns false in case no rule is found for selected URL

  • access - public

Method toUrl (line 148)

string toUrl( [array $params = array()])

Generates a custom URL, depending on current rewrite rules.

Generates a custom URL, depending on current rewrite rules.

Parameters

  • array $params: An array with parameters to include in the url.
      1. array('controller'=>'post','action'=>'view','id'=>'10')
      1. array('controller'=>'page','action'=>'view_page','webpage'=>'contact_us')

Info

  • return -

    Having the following rewrite rules:

    1.  $Router =new AkRouter();
    2.  
    3.  $Router->map('/setup/*config_settings',array('controller'=>'setup'));
    4.  $Router->map('/customize/*options/:action',array('controller'=>'themes','options'=>3));
    5.  $Router->map('/blog/:action/:id',array('controller'=>'post','action'=>'list','id'=>OPTIONAL),array('id'=>'/\d{1,}/'));
    6.  $Router->map('/:year/:month/:day'array('controller' => 'articles','action' => 'view_headlines','year' => COMPULSORY,'month' => 'all','day' => OPTIONALarray('year'=>'/(20){1}\d{2}/','month'=>'/((1)?\d{1,2}){2}/','day'=>'/(([1-3])?\d{1,2}){2}/'));
    7.  $Router->map('/:webpage'array('controller' => 'page''action' => 'view_page''webpage' => 'index'),array('webpage'=>'/(\w|_)+/'));
    8.  $Router->map('/'array('controller' => 'page''action' => 'view_page''webpage'=>'index'));
    9.  $Router->map('/:controller/:action/:id');

    We get the following results:

    1. $Router->toUrl(array('controller'=>'page','action'=>'view_page','webpage'=>'contact_us'));
    Produces: /contact_us/

    1. $Router->toUrl(array('controller'=>'page','action'=>'view_page','webpage'=>'index'));
    Produces: /

    1. $Router->toUrl(array('controller'=>'post','action'=>'list','id'=>null));
    Produces: /blog/

    1. $Router->toUrl(array('controller'=>'post','action'=>'view','id'=>null));
    Produces: /blog/view/

    1. $Router->toUrl(array('controller'=>'post','action'=>'view','id'=>'10'));
    Produces: /blog/view/10/

    1. $Router->toUrl(array('controller'=>'blog','action'=>'view','id'=>'newest'));
    Produces: /blog/view/newest/

    1. $Router->toUrl(array('controller' => 'articles','action' => 'view_headlines','year' => '2005','month' => '10''day' => null));
    Produces: /2005/10/

    1. $Router->toUrl(array('controller'
    => 'articles','action' => 'view_headlines','year' => '2006','month' => 'all', 'day' => null));</code> Produces: /2006/

    1. $Router->toUrl(array('controller' => 'user','action' => 'list','id' => '12'));
    Produces: /user/list/12/

    1. $Router->toUrl(array('controller' => 'setup','config_settings' => array('themes','clone','12')));
    Produces: /setup/themes/clone/12/

    1. $Router->toUrl(array('controller' => 'themes','options' => array('blue','css','sans_serif')'action'=>'clone'));
    Produces: /customize/blue/css/sans_serif/clone/

  • access - public

Method _loadUrlRewriteSettings (line 643)

void _loadUrlRewriteSettings( )

This method tries to determine if url rewrite is enabled on this server.

It has only been tested on apache. It is strongly recomended that you manually define the constant AK_URL_REWRITE_ENABLED on your config file to the avoid overload this function causes and to prevent from missfunctioning

Info

Method _urlDecode (line 609)

void _urlDecode( $input)

Url decode a string or an array of strings

Parameters

  • $input:

Info

Method _urlEncode (line 624)

void _urlEncode( $input)

Url encodes a string or an array of strings

Parameters

  • $input:

Info

Inherited Variables

Inherited Class Variable Summary

Inherited Methods

Inherited Method Summary

Inherited From Class AkObject

AkObject::AkObject() - A hack to support __construct() on PHP 4

AkObject::__construct() - Class constructor, overriden in descendant classes

AkObject::freeMemory() - Unsets circular reference children that are not freed from memory when calling unset() or when the parent object is garbage collected.

AkObject::log() -

AkObject::toString() - Object-to-string conversion

AkObject::__clone() - Clone class (Zend Engine 2 compatibility trick)

AkObject::__destruct() - Class destructor, overriden in descendant classes

AkObject::__toString() -



Documentation generated on Tue, 17 Jun 2008 14:26:42 +0200 by phpDocumentor 1.3.2