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.
파일 보기
프로젝트 열기: unionofrad/lithium
1 사용 예제들
보호된 프로퍼티들
프로퍼티 |
타입 |
설명 |
|
$_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. |
|
메소드 상세
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`. |
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. |
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. |
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`. |
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 |
|