PHP Класс 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.
Показать файл
Открыть проект
Примеры использования класса
Защищенные свойства (Protected)
Свойство |
Тип |
Описание |
|
$_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) {
...
}
));
|
|
Открытые методы
Метод |
Описание |
|
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). |
|
Защищенные методы
Метод |
Описание |
|
_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. |
|
Описание методов
_call()
защищенный статический Метод
Invokes the callable object returned by _callable(), and returns the results, usually a
Response object instance.
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`. |
Результат |
mixed |
Returns the return value of `$callable`, usually an instance of
`lithium\action\Response`. |
_callable()
защищенный статический Метод
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. |
Результат |
object |
Returns a callable object which the request will be routed to. |
applyRules()
публичный статический Метод
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. |
Результат |
array |
Returns the `$params` array with formatting rules applied to array values. |
config()
публичный статический Метод
Used to set configuration parameters for the Dispatcher.
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. |
Результат |
array |
If no parameters are passed, returns an associative array with the current
configuration, otherwise returns `null`. |
run()
публичный статический Метод
Dispatches a request based on a request object (an instance or subclass of
lithium\net\http\Request).
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 |
|
Результат |
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`. |
Описание свойств
$_classes защищенное статическое свойство
Fully-namespaced router class reference. Class must implement a parse() method,
which must return an array with (at a minimum) 'controller' and 'action' keys.
protected static array $_classes |
Результат |
array |
|
$_rules защищенное статическое свойство
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) {
...
}
));
protected static array $_rules |
Результат |
array |
|