ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
Slim_Route Class Reference
+ Collaboration diagram for Slim_Route:

Public Member Functions

 __construct ( $pattern, $callable)
 Constructor. More...
 
 getPattern ()
 Get route pattern. More...
 
 setPattern ( $pattern)
 Set route pattern. More...
 
 getCallable ()
 Get route callable. More...
 
 setCallable ($callable)
 Set route callable. More...
 
 getConditions ()
 Get route conditions. More...
 
 setConditions (array $conditions)
 Set route conditions. More...
 
 getName ()
 Get route name. More...
 
 setName ( $name)
 Set route name. More...
 
 getParams ()
 Get route parameters. More...
 
 setHttpMethods ()
 Add supported HTTP method(s) More...
 
 getHttpMethods ()
 Get supported HTTP methods. More...
 
 appendHttpMethods ()
 Append supported HTTP methods. More...
 
 via ()
 Append supported HTTP methods (alias for Route::appendHttpMethods) More...
 
 supportsHttpMethod ( $method)
 Detect support for an HTTP method. More...
 
 getRouter ()
 Get router. More...
 
 setRouter (Slim_Router $router)
 Set router. More...
 
 getMiddleware ()
 Get middleware. More...
 
 setMiddleware ( $middleware)
 Set middleware. More...
 
 matches ( $resourceUri)
 Matches URI? More...
 
 name ( $name)
 Set route name. More...
 
 conditions (array $conditions)
 Merge route conditions. More...
 
 dispatch ()
 Dispatch route. More...
 

Static Public Member Functions

static setDefaultConditions (array $defaultConditions)
 Set default route conditions for all instances. More...
 
static getDefaultConditions ()
 Get default route conditions for all instances. More...
 

Protected Member Functions

 convertPatternToRegex ( $matches)
 Convert a URL parameter (ie. More...
 

Protected Attributes

 $pattern
 
 $callable
 
 $conditions = array()
 
 $name
 
 $params = array()
 
 $methods = array()
 
 $router
 
 $middleware = array()
 

Static Protected Attributes

static $defaultConditions = array()
 

Detailed Description

Definition at line 40 of file Route.php.

Constructor & Destructor Documentation

◆ __construct()

Slim_Route::__construct (   $pattern,
  $callable 
)

Constructor.

Parameters
string$patternThe URL pattern (ie. "/books/:id")
mixed$callableAnything that returns TRUE for is_callable()

Definition at line 92 of file Route.php.

References $callable, $pattern, setCallable(), setConditions(), and setPattern().

92  {
93  $this->setPattern($pattern);
94  $this->setCallable($callable);
95  $this->setConditions(self::getDefaultConditions());
96  }
setCallable($callable)
Set route callable.
Definition: Route.php:145
setPattern( $pattern)
Set route pattern.
Definition: Route.php:128
setConditions(array $conditions)
Set route conditions.
Definition: Route.php:162
+ Here is the call graph for this function:

Member Function Documentation

◆ appendHttpMethods()

Slim_Route::appendHttpMethods ( )

Append supported HTTP methods.

Returns
void

Definition at line 213 of file Route.php.

213  {
214  $args = func_get_args();
215  $this->methods = array_merge($this->methods, $args);
216  }

◆ conditions()

Slim_Route::conditions ( array  $conditions)

Merge route conditions.

Parameters
array$conditionsKey-value array of URL parameter conditions
Returns
Slim_Route

Definition at line 354 of file Route.php.

Referenced by convertPatternToRegex(), and setConditions().

354  {
355  $this->conditions = array_merge($this->conditions, $conditions);
356  return $this;
357  }
$conditions
Definition: Route.php:55
conditions(array $conditions)
Merge route conditions.
Definition: Route.php:354
+ Here is the caller graph for this function:

◆ convertPatternToRegex()

Slim_Route::convertPatternToRegex (   $matches)
protected

Convert a URL parameter (ie.

":id") into a regular expression

Parameters
arrayURL parameters
Returns
string Regular expression for URL parameter

Definition at line 330 of file Route.php.

References conditions().

330  {
331  $key = str_replace(':', '', $matches[0]);
332  if ( array_key_exists($key, $this->conditions) ) {
333  return '(?P<' . $key . '>' . $this->conditions[$key] . ')';
334  } else {
335  return '(?P<' . $key . '>[a-zA-Z0-9_\-\.\!\~\*\\\'\(\)\:\@\&\=\$\+,%]+)';
336  }
337  }
conditions(array $conditions)
Merge route conditions.
Definition: Route.php:354
+ Here is the call graph for this function:

◆ dispatch()

Slim_Route::dispatch ( )

Dispatch route.

This method invokes this route's callable. If middleware is registered for this route, each callable middleware is invoked in the order specified.

This method is smart about trailing slashes on the route pattern. If this route's pattern is defined with a trailing slash, and if the current request URI does not have a trailing slash but otherwise matches this route's pattern, a Slim_Exception_RequestSlash will be thrown triggering an HTTP 301 Permanent Redirect to the same URI with a trailing slash. This Exception is caught in the Slim::run loop. If this route's pattern is defined without a trailing slash, and if the current request URI does have a trailing slash, this route will not be matched and a 404 Not Found response will be sent if no subsequent matching routes are found.

Returns
bool Was route callable invoked successfully?
Exceptions
Slim_Exception_RequestSlash

Definition at line 380 of file Route.php.

References getCallable().

380  {
381  if ( substr($this->pattern, -1) === '/' && substr($this->router->getRequest()->getResourceUri(), -1) !== '/' ) {
382  throw new Slim_Exception_RequestSlash();
383  }
384  //Invoke middleware
385  foreach ( $this->middleware as $mw ) {
386  if ( is_callable($mw) ) {
387  call_user_func($mw);
388  }
389  }
390  //Invoke callable
391  if ( is_callable($this->getCallable()) ) {
392  call_user_func_array($this->callable, array_values($this->params));
393  return true;
394  }
395  return false;
396  }
getCallable()
Get route callable.
Definition: Route.php:136
+ Here is the call graph for this function:

◆ getCallable()

Slim_Route::getCallable ( )

Get route callable.

Returns
mixed

Definition at line 136 of file Route.php.

References $callable.

Referenced by dispatch().

136  {
137  return $this->callable;
138  }
+ Here is the caller graph for this function:

◆ getConditions()

Slim_Route::getConditions ( )

Get route conditions.

Returns
array

Definition at line 153 of file Route.php.

References $conditions.

153  {
154  return $this->conditions;
155  }
$conditions
Definition: Route.php:55

◆ getDefaultConditions()

static Slim_Route::getDefaultConditions ( )
static

Get default route conditions for all instances.

Returns
array

Definition at line 111 of file Route.php.

111  {
112  return self::$defaultConditions;
113  }

◆ getHttpMethods()

Slim_Route::getHttpMethods ( )

Get supported HTTP methods.

Returns
array

Definition at line 205 of file Route.php.

References $methods.

205  {
206  return $this->methods;
207  }

◆ getMiddleware()

Slim_Route::getMiddleware ( )

Get middleware.

Returns
array[Callable]

Definition at line 257 of file Route.php.

References $middleware.

257  {
258  return $this->middleware;
259  }
$middleware
Definition: Route.php:85

◆ getName()

Slim_Route::getName ( )

Get route name.

Returns
string|null

Definition at line 170 of file Route.php.

References $name.

170  {
171  return $this->name;
172  }

◆ getParams()

Slim_Route::getParams ( )

Get route parameters.

Returns
array

Definition at line 188 of file Route.php.

References $params.

188  {
189  return $this->params;
190  }

◆ getPattern()

Slim_Route::getPattern ( )

Get route pattern.

Returns
string

Definition at line 119 of file Route.php.

References $pattern.

119  {
120  return $this->pattern;
121  }

◆ getRouter()

Slim_Route::getRouter ( )

Get router.

Returns
Slim_Router

Definition at line 240 of file Route.php.

References $router.

240  {
241  return $this->router;
242  }

◆ matches()

Slim_Route::matches (   $resourceUri)

Matches URI?

Parse this route's pattern, and then compare it to an HTTP resource URI This method was modeled after the techniques demonstrated by Dan Sosedoff at:

http://blog.sosedoff.com/2009/09/20/rails-like-php-url-router/

Parameters
string$resourceUriA Request URI
Returns
bool

Definition at line 298 of file Route.php.

References array.

298  {
299  //Extract URL params
300  preg_match_all('@:([\w]+)@', $this->pattern, $paramNames, PREG_PATTERN_ORDER);
301  $paramNames = $paramNames[0];
302 
303  //Convert URL params into regex patterns, construct a regex for this route
304  $patternAsRegex = preg_replace_callback('@:[\w]+@', array($this, 'convertPatternToRegex'), $this->pattern);
305  if ( substr($this->pattern, -1) === '/' ) {
306  $patternAsRegex = $patternAsRegex . '?';
307  }
308  $patternAsRegex = '@^' . $patternAsRegex . '$@';
309 
310  //Cache URL params' names and values if this route matches the current HTTP request
311  if ( preg_match($patternAsRegex, $resourceUri, $paramValues) ) {
312  array_shift($paramValues);
313  foreach ( $paramNames as $index => $value ) {
314  $val = substr($value, 1);
315  if ( isset($paramValues[$val]) ) {
316  $this->params[$val] = urldecode($paramValues[$val]);
317  }
318  }
319  return true;
320  } else {
321  return false;
322  }
323  }
Create styles array
The data for the language used.

◆ name()

Slim_Route::name (   $name)

Set route name.

Parameters
string$nameThe name of the route
Returns
Slim_Route

Definition at line 344 of file Route.php.

References $name, and setName().

Referenced by setName().

344  {
345  $this->setName($name);
346  return $this;
347  }
setName( $name)
Set route name.
Definition: Route.php:179
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setCallable()

Slim_Route::setCallable (   $callable)

Set route callable.

Parameters
mixed$callable
Returns
void

Definition at line 145 of file Route.php.

References $callable.

Referenced by __construct().

145  {
146  $this->callable = $callable;
147  }
+ Here is the caller graph for this function:

◆ setConditions()

Slim_Route::setConditions ( array  $conditions)

Set route conditions.

Parameters
array$conditions
Returns
void

Definition at line 162 of file Route.php.

References $conditions, and conditions().

Referenced by __construct().

162  {
163  $this->conditions = $conditions;
164  }
$conditions
Definition: Route.php:55
conditions(array $conditions)
Merge route conditions.
Definition: Route.php:354
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setDefaultConditions()

static Slim_Route::setDefaultConditions ( array  $defaultConditions)
static

Set default route conditions for all instances.

Parameters
array$defaultConditions
Returns
void

Definition at line 103 of file Route.php.

References $defaultConditions.

103  {
104  self::$defaultConditions = $defaultConditions;
105  }
static $defaultConditions
Definition: Route.php:60

◆ setHttpMethods()

Slim_Route::setHttpMethods ( )

Add supported HTTP method(s)

Returns
void

Definition at line 196 of file Route.php.

196  {
197  $args = func_get_args();
198  $this->methods = $args;
199  }

◆ setMiddleware()

Slim_Route::setMiddleware (   $middleware)

Set middleware.

This method allows middleware to be assigned to a specific Route. If the method argument is_callable (including callable arrays!), we directly append the argument to $this->middleware. Else, we assume the argument is an array of callables and merge the array with $this->middleware. Even if non-callables are included in the argument array, we still merge them; we lazily check each item against is_callable during Route::dispatch().

Parameters
Callable|array[Callable]
Returns
Slim_Route
Exceptions
InvalidArgumentExceptionIf argument is not callable or not an array

Definition at line 276 of file Route.php.

References $middleware.

276  {
277  if ( is_callable($middleware) ) {
278  $this->middleware[] = $middleware;
279  } else if ( is_array($middleware) ) {
280  $this->middleware = array_merge($this->middleware, $middleware);
281  } else {
282  throw new InvalidArgumentException('Route middleware must be callable or an array of callables');
283  }
284  return $this;
285  }
$middleware
Definition: Route.php:85

◆ setName()

Slim_Route::setName (   $name)

Set route name.

Parameters
string$name
Returns
void

Definition at line 179 of file Route.php.

References $name, name(), and string.

Referenced by name().

179  {
180  $this->name = (string)$name;
181  $this->router->cacheNamedRoute($this->name, $this);
182  }
Add rich text string
The name of the decorator.
name( $name)
Set route name.
Definition: Route.php:344
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setPattern()

Slim_Route::setPattern (   $pattern)

Set route pattern.

Parameters
string$pattern
Returns
void

Definition at line 128 of file Route.php.

References $pattern.

Referenced by __construct().

128  {
129  $this->pattern = str_replace(')', ')?', (string)$pattern);
130  }
+ Here is the caller graph for this function:

◆ setRouter()

Slim_Route::setRouter ( Slim_Router  $router)

Set router.

Parameters
Slim_Router$router
Returns
void

Definition at line 249 of file Route.php.

References $router.

249  {
250  $this->router = $router;
251  }

◆ supportsHttpMethod()

Slim_Route::supportsHttpMethod (   $method)

Detect support for an HTTP method.

Returns
bool

Definition at line 232 of file Route.php.

232  {
233  return in_array($method, $this->methods);
234  }

◆ via()

Slim_Route::via ( )

Append supported HTTP methods (alias for Route::appendHttpMethods)

Returns
Slim_Route

Definition at line 222 of file Route.php.

222  {
223  $args = func_get_args();
224  $this->methods = array_merge($this->methods, $args);
225  return $this;
226  }

Field Documentation

◆ $callable

Slim_Route::$callable
protected

Definition at line 50 of file Route.php.

Referenced by __construct(), getCallable(), and setCallable().

◆ $conditions

Slim_Route::$conditions = array()
protected

Definition at line 55 of file Route.php.

Referenced by getConditions(), and setConditions().

◆ $defaultConditions

Slim_Route::$defaultConditions = array()
staticprotected

Definition at line 60 of file Route.php.

Referenced by setDefaultConditions().

◆ $methods

Slim_Route::$methods = array()
protected

Definition at line 75 of file Route.php.

Referenced by getHttpMethods().

◆ $middleware

Slim_Route::$middleware = array()
protected

Definition at line 85 of file Route.php.

Referenced by getMiddleware(), and setMiddleware().

◆ $name

Slim_Route::$name
protected

Definition at line 65 of file Route.php.

Referenced by getName(), name(), and setName().

◆ $params

Slim_Route::$params = array()
protected

Definition at line 70 of file Route.php.

Referenced by getParams().

◆ $pattern

Slim_Route::$pattern
protected

Definition at line 45 of file Route.php.

Referenced by __construct(), getPattern(), and setPattern().

◆ $router

Slim_Route::$router
protected

Definition at line 80 of file Route.php.

Referenced by getRouter(), and setRouter().


The documentation for this class was generated from the following file: