PHP Class lithium\action\Dispatcher

After either receiving or instantiating a Request object instance, the Dispatcher passes that instance to the Router, which produces the parameters necessary to dispatch the request (unless no route matches, in which case an exception is thrown). Using these parameters, the Dispatcher loads and instantiates the correct Controller object, and passes it the Request object instance. The Controller returns a Response object to the Dispatcher, where the headers and content are rendered and sent to the browser.
See also: lithium\net\http\Router
See also: lithium\action\Request
See also: lithium\action\Response
See also: lithium\action\Controller
Inheritance: extends lithium\core\StaticObject
Datei anzeigen Open project: unionofrad/lithium Class Usage Examples

Protected Properties

Property Type Description
$_classes array Fully-namespaced router class reference. Class must implement a parse() method, which must return an array with (at a minimum) 'controller' and 'action' keys.
$_rules array Each key in the array represents a 'rule'; if a key that matches the rule is present (and not empty) in a route, (i.e. the result of Router::parse()) then the rule's value will be applied to the route before it is dispatched. When applying a rule, any array elements of the flag which are present in the route will be modified using a String::insert()-formatted string. Alternatively, a callback can be used to do custom transformations other than the default String::insert(). For example, to implement action prefixes (i.e. admin_index), set a rule named 'admin', with a value array containing a modifier key for the action element of a route, i.e.: array('action' => 'admin_{:action}'). Now, if the 'admin' key is present and not empty in the parameters returned from routing, the value of 'action' will be rewritten per the settings in the rule: Dispatcher::config(array( 'rules' => array( 'admin' => 'admin_{:action}' ) )); The following example shows two rules that continuously or independently transform the action parameter in order to allow any variations i.e. 'admin_index', 'api_index' and 'admin_api_index'. ... 'api' => 'api_{:action}', 'admin' => 'admin_{:action}' ... Here's another example. To support normalizing actions, set a rule named 'action' with a value array containing a callback that uses Inflector to camelize the action: ... 'action' => array('action' => function($params) { return Inflector::camelize(strtolower($params['action']), false); }) ... The entires rules can become a callback as well: Dispatcher::config(array( 'rules' => function($params) { ... } ));

Public Methods

Method Description
applyRules ( array &$params ) : array Attempts to apply a set of formatting rules from $_rules to a $params array, where each formatting rule is applied if the key of the rule in $_rules is present and not empty in $params. Also performs sanity checking against $params to ensure that no value matching a rule is present unless the rule check passes.
config ( array $config = [] ) : array Used to set configuration parameters for the Dispatcher.
run ( object $request, array $options = [] ) : mixed Dispatches a request based on a request object (an instance or subclass of lithium\net\http\Request).

Protected Methods

Method Description
_call ( object $callable, object $request, array $params ) : mixed Invokes the callable object returned by _callable(), and returns the results, usually a Response object instance.
_callable ( object $request, array $params, array $options ) : object Accepts parameters generated by the Router class in Dispatcher::run(), and produces a callable controller object. By default, this method uses the 'controller' path lookup configuration in Libraries::locate() to return a callable object.

Method Details

_call() protected static method

Invokes the callable object returned by _callable(), and returns the results, usually a Response object instance.
See also: lithium\action
protected static _call ( object $callable, object $request, array $params ) : mixed
$callable object Typically a closure or instance of `lithium\action\Controller`.
$request object An instance of `lithium\action\Request`.
$params array An array of parameters to pass to `$callable`, along with `$request`.
return mixed Returns the return value of `$callable`, usually an instance of `lithium\action\Response`.

_callable() protected static method

Accepts parameters generated by the Router class in Dispatcher::run(), and produces a callable controller object. By default, this method uses the 'controller' path lookup configuration in Libraries::locate() to return a callable object.
protected static _callable ( object $request, array $params, array $options ) : object
$request object The instance of the `Request` class either passed into or generated by `Dispatcher::run()`.
$params array The parameter array generated by routing the request.
$options array Not currently implemented.
return object Returns a callable object which the request will be routed to.

applyRules() public static method

Attempts to apply a set of formatting rules from $_rules to a $params array, where each formatting rule is applied if the key of the rule in $_rules is present and not empty in $params. Also performs sanity checking against $params to ensure that no value matching a rule is present unless the rule check passes.
public static applyRules ( array &$params ) : array
$params array An array of route parameters to which rules will be applied.
return array Returns the `$params` array with formatting rules applied to array values.

config() public static method

Used to set configuration parameters for the Dispatcher.
See also: lithium\action\Dispatcher::$_rules
public static config ( array $config = [] ) : array
$config array Possible key settings are `'classes'` which sets the class dependencies for `Dispatcher` (i.e. `'request'` or `'router'`) and `'rules'`, which sets the pre-processing rules for routing parameters. For more information on the `'rules'` setting, see the `$_rules` property.
return array If no parameters are passed, returns an associative array with the current configuration, otherwise returns `null`.

run() public static method

Dispatches a request based on a request object (an instance or subclass of lithium\net\http\Request).
See also: lithium\action\Request
See also: lithium\action\Response
public static run ( object $request, array $options = [] ) : mixed
$request object An instance of a request object (usually `lithium\action\Request`) with HTTP request information.
$options array
return mixed Returns the value returned from the callable object retrieved from `Dispatcher::_callable()`, which is either a string or an instance of `lithium\action\Response`.

Property Details

$_classes protected_oe static_oe property

Fully-namespaced router class reference. Class must implement a parse() method, which must return an array with (at a minimum) 'controller' and 'action' keys.
See also: lithium\net\http\Router::parse()
protected static array $_classes
return array

$_rules protected_oe static_oe property

Each key in the array represents a 'rule'; if a key that matches the rule is present (and not empty) in a route, (i.e. the result of Router::parse()) then the rule's value will be applied to the route before it is dispatched. When applying a rule, any array elements of the flag which are present in the route will be modified using a String::insert()-formatted string. Alternatively, a callback can be used to do custom transformations other than the default String::insert(). For example, to implement action prefixes (i.e. admin_index), set a rule named 'admin', with a value array containing a modifier key for the action element of a route, i.e.: array('action' => 'admin_{:action}'). Now, if the 'admin' key is present and not empty in the parameters returned from routing, the value of 'action' will be rewritten per the settings in the rule: Dispatcher::config(array( 'rules' => array( 'admin' => 'admin_{:action}' ) )); The following example shows two rules that continuously or independently transform the action parameter in order to allow any variations i.e. 'admin_index', 'api_index' and 'admin_api_index'. ... 'api' => 'api_{:action}', 'admin' => 'admin_{:action}' ... Here's another example. To support normalizing actions, set a rule named 'action' with a value array containing a callback that uses Inflector to camelize the action: ... 'action' => array('action' => function($params) { return Inflector::camelize(strtolower($params['action']), false); }) ... The entires rules can become a callback as well: Dispatcher::config(array( 'rules' => function($params) { ... } ));
See also: lithium\action\Dispatcher::config()
See also: lithium\util\String::insert()
See also: lithium\util\Inflector
protected static array $_rules
return array