\Horde_Routes_Utils

Utility functions for use in templates and controllers

Summary

Methods
Properties
Constants
__construct()
urlFor()
redirectTo()
printRoutes()
controllerScan()
urlQuote()
longestFirst()
arraySubtract()
$mapper
$mapperDict
$redirect
No constants found
No protected methods found
No protected properties found
N/A
_screenArgs()
_subdomainCheck()
No private properties found
N/A

Properties

$mapperDict

$mapperDict : array

Match data from last match; implements for urlFor() route memory

Type

array

$redirect

$redirect : callback

Callback function used for redirectTo()

Type

callback

Methods

__construct()

__construct(\Horde_Routes_Mapper  $mapper, callback  $redirect = null) 

Constructor

Parameters

\Horde_Routes_Mapper $mapper

Mapper for these utilities

callback $redirect

Redirect callback for redirectTo()

urlFor()

urlFor(  $first = array(),   $second = array()) 

Generates a URL.

All keys given to urlFor are sent to the Routes Mapper instance for generation except for::

anchor          specified the anchor name to be appened to the path
host            overrides the default (current) host if provided
protocol        overrides the default (current) protocol if provided
qualified       creates the URL with the host/port information as
                needed

The URL is generated based on the rest of the keys. When generating a new URL, values will be used from the current request's parameters (if present). The following rules are used to determine when and how to keep the current requests parameters:

  • If the controller is present and begins with '/', no defaults are used
  • If the controller is changed, action is set to 'index' unless otherwise specified

For example, if the current request yielded a dict (associative array) of array('controller'=>'blog', 'action'=>'view', 'id'=>2), with the standard ':controller/:action/:id' route, you'd get the following results::

urlFor(array('id'=>4))                    =>  '/blog/view/4',
urlFor(array('controller'=>'/admin'))     =>  '/admin',
urlFor(array('controller'=>'admin'))      =>  '/admin/view/2'
urlFor(array('action'=>'edit'))           =>  '/blog/edit/2',
urlFor(array('action'=>'list', id=NULL))  =>  '/blog/list'

Static and Named Routes

If there is a string present as the first argument, a lookup is done against the named routes table to see if there's any matching routes. The keyword defaults used with static routes will be sent in as GET query arg's if a route matches.

If no route by that name is found, the string is assumed to be a raw URL. Should the raw URL begin with / then appropriate SCRIPT_NAME data will be added if present, otherwise the string will be used as the url with keyword args becoming GET query args.

Parameters

$first
$second

redirectTo()

redirectTo(mixed  $first = array(), mixed  $second = array()) : mixed

Issues a redirect based on the arguments.

Redirects should occur as a "302 Moved" header, however the web framework may utilize a different method.

All arguments are passed to urlFor() to retrieve the appropriate URL, then the resulting URL it sent to the redirect function as the URL.

Parameters

mixed $first

First argument in varargs, same as urlFor()

mixed $second

Second argument in varargs

Returns

mixed —

Result of redirect callback

printRoutes()

printRoutes(\stream|null  $stream = null, string|null  $eol = PHP_EOL) : void

Pretty-print a listing of the routes connected to the mapper.

Parameters

\stream|null $stream

Output stream for printing (optional)

string|null $eol

Line ending (optional)

controllerScan()

controllerScan(string  $dirname = null, string  $prefix = '') : array

Scan a directory for PHP files and use them as controllers. Used as the default scanner callback for Horde_Routes_Mapper. See the constructor of that class for more information.

Given a directory with: foo.php, bar.php, baz.php Returns an array: foo, bar, baz

Parameters

string $dirname

Directory to scan for controller files

string $prefix

Prefix controller names (optional)

Returns

array —

Array of controller names

urlQuote()

urlQuote(string  $url, string  $encoding = null) 

Quote a string containing a URL in a given encoding.

Parameters

string $url

URL to encode

string $encoding

Encoding to use

longestFirst()

longestFirst(string  $fst, string  $lst) : integer

Callback used by usort() in controllerScan() to sort controller names by the longest first.

Parameters

string $fst

First string to compare

string $lst

Last string to compare

Returns

integer —

Difference of string length (first - last)

arraySubtract()

arraySubtract(  $a1,   $a2) 

Parameters

$a1
$a2

_screenArgs()

_screenArgs(  $kargs) 

Private function that takes a dict, and screens it against the current request dict to determine what the dict should look like that is used.

This is responsible for the requests "memory" of the current.

Parameters

$kargs

_subdomainCheck()

_subdomainCheck(  $kargs) 

Screen the kargs for a subdomain and alter it appropriately depending on the current subdomain or lack therof.

Parameters

$kargs