\Horde_Routes_Mapper

The mapper class handles URL generation and recognition for web applications

The mapper class is built by handling associated arrays of information and passing associated arrays back to the application for it to handle and dispatch the appropriate scripts.

Summary

Methods
Properties
Constants
__construct()
connect()
setCache()
createRegs()
match()
routematch()
generate()
resource()
$environ
$controllerScan
$directory
$alwaysScan
$explicit
$debug
$subDomains
$subDomainsIgnore
$appendSlash
$prefix
$matchList
$routeNames
$urlCache
$encoding
$decodeErrors
$domainMatch
$maxKeys
$minKeys
$utils
$cache
$cacheLifetime
No constants found
_createGens()
_match()
_requirementsFor()
_swap()
_keysort()
_keycmp()
_arrayUnion()
_cmp()
_stripSlashes()
$_createdRegs
$_createdGens
$_gendict
$_keysortTmp
$_regPrefix
N/A
No private methods found
No private properties found
N/A

Properties

$environ

$environ : array

Filtered request environment with keys like SCRIPT_NAME

Type

array

$controllerScan

$controllerScan : callback

Callback function used to get array of controller names

Type

callback

$directory

$directory : string

Path to controller directory passed to controllerScan function

Type

string

$alwaysScan

$alwaysScan : boolean

Call controllerScan callback before every route match?

Type

boolean

$explicit

$explicit : boolean

Disable route memory and implicit defaults?

Type

boolean

$debug

$debug : boolean

Collect debug information during route match?

Type

boolean

$subDomains

$subDomains : boolean

Use sub-domain support?

Type

boolean

$subDomainsIgnore

$subDomainsIgnore : array

Array of sub-domains to ignore if using sub-domain support

Type

array

$appendSlash

$appendSlash : boolean

Append trailing slash ('/') to generated routes?

Type

boolean

$prefix

$prefix : null|string

Prefix to strip during matching and to append during generation

Type

null|string

$matchList

$matchList : array

Array of connected routes

Type

array

$routeNames

$routeNames : array

Array of connected named routes, indexed by name

Type

array

$urlCache

$urlCache : array

Cache of URLs used in generate()

Type

array

$encoding

$encoding : string

Encoding of routes URLs (not yet supported)

Type

string

$decodeErrors

$decodeErrors : string

What to do on decoding errors? 'ignore' or 'replace'

Type

string

$domainMatch

$domainMatch : string

Partial regexp used to match domain part of the end of URLs to match

Type

string

$maxKeys

$maxKeys : array

Array of all connected routes, indexed by the serialized array of all keys that each route could utilize.

Type

array

$minKeys

$minKeys : array

Array of all connected routes, indexed by the serialized array of the minimum keys that each route needs.

Type

array

$utils

$utils : \Horde_Routes_Utils

Utility functions like urlFor() and redirectTo() for this Mapper

Type

\Horde_Routes_Utils

$cache

$cache : \Horde_Cache

Cache

Type

\Horde_Cache

$cacheLifetime

$cacheLifetime : integer

Cache lifetime for the same value of $this->matchList

Type

integer

$_createdRegs

$_createdRegs : boolean

Have regular expressions been created for all connected routes?

Type

boolean

$_createdGens

$_createdGens : boolean

Have generation hashes been created for all connected routes?

Type

boolean

$_gendict

$_gendict : array

Generation hashes created for all connected routes

Type

array

$_keysortTmp

$_keysortTmp : array

Temporary variable used to pass array of keys into _keysort() callback

Type

array

$_regPrefix

$_regPrefix : string

Regular expression generated to match after the prefix

Type

string

Methods

__construct()

__construct(  $kargs = array()) 

Constructor.

Keyword arguments ($kargs): controllerScan (callback) Function to return an array of valid controllers

redirect (callback) Function to perform a redirect for Horde_Routes_Utils->redirectTo()

directory (string) Path to the directory that will be passed to the controllerScan callback

alwaysScan (boolean) Should the controllerScan callback be called before every URL match?

explicit (boolean) Should routes be connected with the implicit defaults of array('controller'=>'content', 'action'=>'index', 'id'=>null)? When set to True, these will not be added to route connections.

Parameters

$kargs

connect()

connect(mixed  $first, mixed  $second = null, mixed  $third = null) : void

Create and connect a new Route to the Mapper.

Usage: $m = new Horde_Routes_Mapper(); $m->connect(':controller/:action/:id'); $m->connect('date/:year/:month/:day', array('controller' => "blog", 'action' => 'view'); $m->connect('archives/:page', array('controller' => 'blog', 'action' => 'by_page', ' requirements' => array('page' => '\d{1,2}'))); $m->connect('category_list', 'archives/category/:section', array('controller' => 'blog', 'action' => 'category', 'section' => 'home', 'type' => 'list')); $m->connect('home', '', array('controller' => 'blog', 'action' => 'view', 'section' => 'home'));

Parameters

mixed $first

First argument in vargs, see usage above.

mixed $second

Second argument in varags

mixed $third

Third argument in varargs

setCache()

setCache(\Horde_Cache  $cache) 

Set an optional Horde_Cache object for the created rules.

Parameters

\Horde_Cache $cache

Cache object

createRegs()

createRegs(array  $clist = null) : void

Creates the regexes for all connected routes

Parameters

array $clist

controller list, controller_scan will be used otherwise

match()

match(string  $url) 

Match a URL against one of the routes contained.

It will return null if no valid match is found.

Usage: $resultdict = $m->match('/joe/sixpack');

Parameters

string $url

URL to match

routematch()

routematch(string  $url) 

Match a URL against one of the routes contained.

It will return null if no valid match is found, otherwise a result dict (array) and a route object is returned.

Usage: list($resultdict, $resultobj) = $m->match('/joe/sixpack');

Parameters

string $url

URL to match

generate()

generate(  $first = null,   $second = null) : null|string

Generates the URL from a given set of keywords Returns the URL text, or null if no URL could be generated.

Usage: $m->generate(array('controller' => 'content', 'action' => 'view', 'id' => 10));

Parameters

$first
$second

Returns

null|string —

URL text or null

resource()

resource(string  $memberName, string  $collectionName, array  $kargs = array()) : void

Generate routes for a controller resource

The $memberName name should be the appropriate singular version of the resource given your locale and used with members of the collection.

The $collectionName name will be used to refer to the resource collection methods and should be a plural version of the $memberName argument. By default, the $memberName name will also be assumed to map to a controller you create.

The concept of a web resource maps somewhat directly to 'CRUD' operations. The overlying things to keep in mind is that mapping a resource is about handling creating, viewing, and editing that resource.

All keyword arguments ($kargs) are optional.

controller If specified in the keyword args, the controller will be the actual controller used, but the rest of the naming conventions used for the route names and URL paths are unchanged.

collection Additional action mappings used to manipulate/view the entire set of resources provided by the controller.

Example::

    $map->resource('message', 'messages',
                   array('collection' => array('rss' => 'GET)));
    # GET /message;rss (maps to the rss action)
    # also adds named route "rss_message"

member Additional action mappings used to access an individual 'member' of this controllers resources.

 Example::

     $map->resource('message', 'messages',
                    array('member' => array('mark' => 'POST')));
     # POST /message/1;mark (maps to the mark action)
     # also adds named route "mark_message"

new Action mappings that involve dealing with a new member in the controller resources.

 Example::

     $map->resource('message', 'messages',
                    array('new' => array('preview' => 'POST')));
     # POST /message/new;preview (maps to the preview action)
     # also adds a url named "preview_new_message"

pathPrefix Prepends the URL path for the Route with the pathPrefix given. This is most useful for cases where you want to mix resources or relations between resources.

namePrefix Perpends the route names that are generated with the namePrefix given. Combined with the pathPrefix option, it's easy to generate route names and paths that represent resources that are in relations.

 Example::

     map.resource('message', 'messages',
                  array('controller' => 'categories',
                        'pathPrefix' => '/category/:category_id',
                        'namePrefix' => 'category_')));
         # GET /category/7/message/1
         # has named route "category_message"

parentResource An assoc. array containing information about the parent resource, for creating a nested resource. It should contain the $memberName and collectionName of the parent resource. This assoc. array will be available via the associated Route object which can be accessed during a request via request.environ['routes.route']

 If ``parentResource`` is supplied and ``pathPrefix`` isn't,
 ``pathPrefix`` will be generated from ``parentResource`` as
 "<parent collection name>/:<parent member name>_id".

 If ``parentResource`` is supplied and ``namePrefix`` isn't,
 ``namePrefix`` will be generated from ``parentResource`` as
 "<parent member name>_".

 Example::

     $m = new Horde_Routes_Mapper();
     $utils = $m->utils;

     $m->resource('location', 'locations',
                  array('parentResource' =>
                         array('memberName' => 'region',
                               'collectionName' => 'regions'))));
     # pathPrefix is "regions/:region_id"
     # namePrefix is "region_"

     $utils->urlFor('region_locations', array('region_id'=>13));
     # '/regions/13/locations'

     $utils->urlFor('region_new_location', array('region_id'=>13));
     # '/regions/13/locations/new'

     $utils->urlFor('region_location',
                   array('region_id'=>13, 'id'=>60));
     # '/regions/13/locations/60'

     $utils->urlFor('region_edit_location',
                   array('region_id'=>13, 'id'=>60));
     # '/regions/13/locations/60/edit'

Overriding generated pathPrefix::

 $m = new Horde_Routes_Mapper();
 $utils = new Horde_Routes_Utils();

 $m->resource('location', 'locations',
              array('parentResource' =>
                    array('memberName' => 'region',
                          'collectionName' => 'regions'),
                    'pathPrefix' => 'areas/:area_id')));
  # name prefix is "region_"

  $utils->urlFor('region_locations', array('area_id'=>51));
  # '/areas/51/locations'

Overriding generated namePrefix::

  $m = new Horde_Routes_Mapper
 $m->resource('location', 'locations',
              array('parentResource' =>
                    array('memberName' => 'region',
                          'collectionName' => 'regions'),
                    'namePrefix' => '')));
  # pathPrefix is "regions/:region_id"

  $utils->urlFor('locations', array('region_id'=>51));
  # '/regions/51/locations'

Note: Since Horde Routes 0.2.0 and Python Routes 1.8, this method is not compatible with earlier versions inasmuch as the semicolon is no longer used to delimit custom actions. This was a change in Rails itself (http://dev.rubyonrails.org/changeset/6485) and adopting it here allows us to keep parity with Rails and ActiveResource.

Parameters

string $memberName

Singular version of the resource name

string $collectionName

Collection name (plural of $memberName)

array $kargs

Keyword arguments (see above)

_createGens()

_createGens() : void

Create the generation hashes (arrays) for route lookups

_match()

_match(string  $url) : null|array

Internal Route matcher

Matches a URL against a route, and returns a tuple (array) of the match dict (array) and the route object if a match is successful, otherwise it returns null.

Parameters

string $url

URL to match

Returns

null|array —

Match data if matched, otherwise null

_requirementsFor()

_requirementsFor(  $meth, array  $options) : \$options

Returns a new dict to be used for all route creation as the route options.

Parameters

$meth
array $options

Assoc. array to populate with 'conditions' key

Returns

\$options —

populated

_swap()

_swap(array  $dct, array  $newdct) : array

Swap the keys and values in the dict, and uppercase the values from the dict during the swap.

Parameters

array $dct

Input dict (assoc. array)

array $newdct

Output dict to populate

Returns

array —

$newdct populated

_keysort()

_keysort(array  $array) : void

Sort an array of Horde_Routes_Routes to using _keycmp() for the comparision to order them ideally for matching.

An unfortunate property of PHP's usort() is that if two members compare equal, their order in the sorted array is undefined (see PHP manual). This is unsuitable for us because the order that the routes were connected to the mapper is significant.

Uses this method uses merge sort algorithm based on the comments in http://www.php.net/usort

Parameters

array $array

Array Horde_Routes_Route objects to sort (by reference)

_keycmp()

_keycmp(array  $a, array  $b) : integer

Compare two Horde_Route_Routes objects by their keys against the instance variable $keysortTmp. Used by _keysort().

Parameters

array $a

First dict (assoc. array)

array $b

Second dict

Returns

integer

_arrayUnion()

_arrayUnion(array  $a, array  $b) : array

Create a union of two arrays.

Parameters

array $a

First array

array $b

Second array

Returns

array —

Union of $a and $b

_cmp()

_cmp(integer|float  $a, integer|\flot  $b) 

Equivalent of Python's cmp() function.

Parameters

integer|float $a

First item to compare

integer|\flot $b

Second item to compare

_stripSlashes()

_stripSlashes(string  $name) : string

Trims slashes from the beginning or end of a part/URL.

Parameters

string $name

Part or URL with slash at begin/end

Returns

string —

Part or URL with begin/end slashes removed