Dispatcher
final class Dispatcher (View source)
\Yaf\Dispatcher purpose is to initialize the request environment, route the incoming request, and then dispatch any discovered actions; it aggregates any responses and returns them when the process is complete.
\Yaf\Dispatcher also implements the Singleton pattern, meaning only a single instance of it may be available at any given time. This allows it to also act as a registry on which the other objects in the dispatch process may draw.
Properties
static protected Dispatcher | $_instance | ||
protected Router | $_router | ||
protected View_Interface | $_view | ||
protected Request_Abstract | $_request | ||
protected Plugin_Abstract | $_plugins | ||
protected bool | $_auto_render | ||
protected string | $_return_response | ||
protected string | $_instantly_flush | ||
protected string | $_default_module | ||
protected string | $_default_controller | ||
protected string | $_default_action |
Methods
enable view rendering
disable view engine, used in some app that user will output by himself
Note:
you can simply return FALSE in a action to prevent the auto-rendering of that action
Initialize view and return it
This method provides a solution for that if you want use a custom view engine instead of \Yaf\View\Simple
Retrieve the \Yaf\Application instance. same as \Yaf\Application::app().
No description
Set error handler for Yaf. when application.dispatcher.throwException is off, Yaf will trigger catch-able error while unexpected errors occurred.
Thus, this error handler will be called while the error raise.
Change default module name
Change default controller name
Change default action name
No description
\Yaf\Dispatcher will render automatically after dispatches an incoming request, you can prevent the rendering by calling this method with $flag TRUE
Note:
you can simply return FALSE in a action to prevent the auto-rendering of that action
Switch on/off the instant flushing
No description
This method does the heavy work of the \Yaf\Dispatcher. It take a request object.
The dispatch process has three distinct events:
- Routing
- Dispatching
- Response
Routing takes place exactly once, using the values in the request object when dispatch() is called. Dispatching takes place in a loop; a request may either indicate multiple actions to dispatch, or the controller or a plugin may reset the request object to force additional actions to dispatch(see \Yaf\Plugin_Abstract. When all is done, the \Yaf\Dispatcher returns a response.
Switch on/off exception throwing while unexpected error occurring. When this is on, Yaf will throwing exceptions instead of triggering catchable errors.
You can also use application.dispatcher.throwException to achieve the same purpose.
While the application.dispatcher.throwException is On(you can also calling to \Yaf\Dispatcher::throwException(TRUE) to enable it), Yaf will throw \Exception whe error occurs instead of trigger error.
then if you enable \Yaf\Dispatcher::catchException()(also can enabled by set application.dispatcher.catchException), all uncaught \Exceptions will be caught by ErrorController::error if you have defined one.
Register a plugin(see \Yaf\Plugin_Abstract). Generally, we register plugins in Bootstrap(see \Yaf\Bootstrap_Abstract).
Details
Dispatcher
enableView()
enable view rendering
bool
disableView()
disable view engine, used in some app that user will output by himself
Note:
you can simply return FALSE in a action to prevent the auto-rendering of that action
View_Interface
initView(string $templates_dir, array $options = null)
Initialize view and return it
Dispatcher
setView(View_Interface $view)
This method provides a solution for that if you want use a custom view engine instead of \Yaf\View\Simple
Dispatcher
setRequest(Request_Abstract $request)
No description
Application
getApplication()
Retrieve the \Yaf\Application instance. same as \Yaf\Application::app().
Router
getRouter()
No description
Request_Abstract
getRequest()
No description
Dispatcher
setErrorHandler(callable $callback, int $error_types)
Set error handler for Yaf. when application.dispatcher.throwException is off, Yaf will trigger catch-able error while unexpected errors occurred.
Thus, this error handler will be called while the error raise.
Dispatcher
setDefaultModule(string $module)
Change default module name
Dispatcher
setDefaultController(string $controller)
Change default controller name
Dispatcher
setDefaultAction(string $action)
Change default action name
Dispatcher
returnResponse(bool $flag)
No description
Dispatcher
autoRender(bool $flag = null)
\Yaf\Dispatcher will render automatically after dispatches an incoming request, you can prevent the rendering by calling this method with $flag TRUE
Note:
you can simply return FALSE in a action to prevent the auto-rendering of that action
Dispatcher
flushInstantly(bool $flag = null)
Switch on/off the instant flushing
static Dispatcher
getInstance()
No description
Response_Abstract
dispatch(Request_Abstract $request)
This method does the heavy work of the \Yaf\Dispatcher. It take a request object.
The dispatch process has three distinct events:
- Routing
- Dispatching
- Response
Routing takes place exactly once, using the values in the request object when dispatch() is called. Dispatching takes place in a loop; a request may either indicate multiple actions to dispatch, or the controller or a plugin may reset the request object to force additional actions to dispatch(see \Yaf\Plugin_Abstract. When all is done, the \Yaf\Dispatcher returns a response.
Dispatcher
throwException(bool $flag = null)
Switch on/off exception throwing while unexpected error occurring. When this is on, Yaf will throwing exceptions instead of triggering catchable errors.
You can also use application.dispatcher.throwException to achieve the same purpose.
Dispatcher
catchException(bool $flag = null)
While the application.dispatcher.throwException is On(you can also calling to \Yaf\Dispatcher::throwException(TRUE) to enable it), Yaf will throw \Exception whe error occurs instead of trigger error.
then if you enable \Yaf\Dispatcher::catchException()(also can enabled by set application.dispatcher.catchException), all uncaught \Exceptions will be caught by ErrorController::error if you have defined one.
Dispatcher
registerPlugin(Plugin_Abstract $plugin)
Register a plugin(see \Yaf\Plugin_Abstract). Generally, we register plugins in Bootstrap(see \Yaf\Bootstrap_Abstract).