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.
Method Detail
Summary:
void
connect
(
string $url_pattern, [
array $options =
array()], [
array $requirements =
null])
void
map
(
$url_pattern, [
$options =
array()], [
$requirements =
null])
string
toUrl
([
array $params =
array()])
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
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
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:
OPTIONAL // 'var_name'=> OPTIONAL, will set 'var_name' as an option
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
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
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:
$Router->map('/setup/*config_settings',array
('controller'=>
'setup'));
$Router->map('/customize/*options/:action',array
('controller'=>
'themes','options'=>3
));
$Router->map('/blog/:action/:id',array
('controller'=>
'post','action'=>
'list','id'=>
OPTIONAL),array
('id'=>
'/\d{1,}/'));
$Router->map('/:year/:month/:day', array
('controller' =>
'articles','action' =>
'view_headlines','year' =>
COMPULSORY,'month' =>
'all','day' =>
OPTIONAL) , array
('year'=>
'/(20){1}\d{2}/','month'=>
'/((1)?\d{1,2}){2}/','day'=>
'/(([1-3])?\d{1,2}){2}/'));
$Router->map('/:webpage', array
('controller' =>
'page', 'action' =>
'view_page', 'webpage' =>
'index'),array
('webpage'=>
'/(\w|_)+/'));
$Router->map('/', array
('controller' =>
'page', 'action' =>
'view_page', 'webpage'=>
'index'));
$Router->map('/:controller/:action/:id');
We get the following results:
Produces: array('controller'=>'page','action'=>'view_page','webpage'=>'contact_us'); Produces: array('controller'=>'page','action'=>'view_page','webpage'=>'index'); Produces: array('controller'=>'page','action'=>'view_page','webpage'=>'index'); Produces: array('controller'=>'post','action'=>'list','id'=>null); Produces: array('controller'=>'post','action'=>'view','id'=>null); Produces: array('controller'=>'post','action'=>'view','id'=>'10');$Router->toParams('/blog/view/newest/');
Produces: array('controller'=>'blog','action'=>'view','id'=>'newest'); Produces: array('controller' => 'articles','action' => 'view_headlines','year' => '2005','month' => '10', 'day' => null); Produces: array('controller' => 'articles','action' => 'view_headlines','year' => '2006','month' => 'all', 'day' => null); Produces: array('controller' => 'user','action' => 'list','id' => '12');$Router->toParams('/setup/themes/clone/12/');
Produces: array('controller' => 'setup','config_settings' => array('themes','clone','12'));$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.
array('controller'=>'post','action'=>'view','id'=>'10')
array('controller'=>'page','action'=>'view_page','webpage'=>'contact_us')
Info
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
Info
Method _urlEncode (line 624)
void _urlEncode(
$input)
Url encodes a string or an array of strings
Parameters
Info