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

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

initView(string $templates_dir, array $options = null)

Initialize view and return it

setView(View_Interface $view)

This method provides a solution for that if you want use a custom view engine instead of \Yaf\View\Simple

setRequest(Request_Abstract $request)

No description

getApplication()

Retrieve the \Yaf\Application instance. same as \Yaf\Application::app().

getRouter()

No description

getRequest()

No description

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.

setDefaultModule(string $module)

Change default module name

setDefaultController(string $controller)

Change default controller name

setDefaultAction(string $action)

Change default action name

returnResponse(bool $flag)

No description

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

flushInstantly(bool $flag = null)

Switch on/off the instant flushing

static Dispatcher
getInstance()

No description

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.

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.

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.

registerPlugin(Plugin_Abstract $plugin)

Register a plugin(see \Yaf\Plugin_Abstract). Generally, we register plugins in Bootstrap(see \Yaf\Bootstrap_Abstract).

Details

Dispatcher enableView()

enable view rendering

Return Value

Dispatcher

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

Return Value

bool

View_Interface initView(string $templates_dir, array $options = null)

Initialize view and return it

Parameters

string $templates_dir
array $options

Return Value

View_Interface

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

Parameters

View_Interface $view

A \Yaf\View_Interface instance

Return Value

Dispatcher

Dispatcher setRequest(Request_Abstract $request)

No description

Parameters

Request_Abstract $request

Return Value

Dispatcher

Application getApplication()

Retrieve the \Yaf\Application instance. same as \Yaf\Application::app().

Return Value

Application

Router getRouter()

No description

Return Value

Router

Request_Abstract getRequest()

No description

Return Value

Request_Abstract

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.

Parameters

callable $callback

a callable callback

int $error_types

YAFERR* constants mask

Return Value

Dispatcher

Dispatcher setDefaultModule(string $module)

Change default module name

Parameters

string $module

Return Value

Dispatcher

Dispatcher setDefaultController(string $controller)

Change default controller name

Parameters

string $controller

Return Value

Dispatcher

Dispatcher setDefaultAction(string $action)

Change default action name

Parameters

string $action

Return Value

Dispatcher

Dispatcher returnResponse(bool $flag)

No description

Parameters

bool $flag

Return Value

Dispatcher

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

Parameters

bool $flag

since 2.2.0, if this parameter is not given, then the current state will be set

Return Value

Dispatcher

Dispatcher flushInstantly(bool $flag = null)

Switch on/off the instant flushing

Parameters

bool $flag

since 2.2.0, if this parameter is not given, then the current state will be set

Return Value

Dispatcher

static Dispatcher getInstance()

No description

Return Value

Dispatcher

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.

Parameters

bool $flag

Return Value

Dispatcher

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.

Parameters

bool $flag

Return Value

Dispatcher

Dispatcher registerPlugin(Plugin_Abstract $plugin)

Register a plugin(see \Yaf\Plugin_Abstract). Generally, we register plugins in Bootstrap(see \Yaf\Bootstrap_Abstract).

Parameters

Plugin_Abstract $plugin

Return Value

Dispatcher