final class Yaf_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 Yaf_Dispatcher $_instance
protected Yaf_Router $_router
protected Yaf_View_Interface $_view
protected Yaf_Request_Abstract $_request
protected Yaf_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

getResponse()

No description

string|null
getDefaultModule()

No description

string|null
getDefaultController()

No description

string|null
getDefaultAction()

No description

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|null $options = null)

Initialize view and return it

setView(Yaf_View_Interface $view)

This method provides a solution for that if you want use a custom view engine instead of Yaf_View_Simple

getApplication()

Retrieve the Yaf_Application instance. same as Yaf_Application::app().

getRouter()

No description

getRequest()

No description

setErrorHandler(callable $callback, int $error_types = YAF_ERR_TYPE_ERROR)

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 Yaf_Dispatcher
getInstance()

No description

dispatch(Yaf_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(Yaf_Plugin_Abstract $plugin)

Register a plugin(see Yaf_Plugin_Abstract). Generally, we register plugins in Bootstrap(see Yaf_Bootstrap_Abstract).

setResponse(Yaf_Response_Abstract $response)

No description

Details

Yaf_Dispatcher enableView()

enable view rendering

Return Value

Yaf_Dispatcher

Yaf_Response_Abstract|null getResponse()

No description

Return Value

Yaf_Response_Abstract|null

string|null getDefaultModule()

No description

Return Value

string|null

string|null getDefaultController()

No description

Return Value

string|null

string|null getDefaultAction()

No description

Return Value

string|null

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

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

Initialize view and return it

Parameters

string $templates_dir
array|null $options

Return Value

Yaf_View_Interface

Yaf_Dispatcher setView(Yaf_View_Interface $view)

This method provides a solution for that if you want use a custom view engine instead of Yaf_View_Simple

Parameters

Yaf_View_Interface $view

A Yaf_View_Interface instance

Return Value

Yaf_Dispatcher

Yaf_Dispatcher setRequest(Yaf_Request_Abstract $request)

No description

Parameters

Yaf_Request_Abstract $request

Return Value

Yaf_Dispatcher

Yaf_Application getApplication()

Retrieve the Yaf_Application instance. same as Yaf_Application::app().

Return Value

Yaf_Application

Yaf_Router getRouter()

No description

Return Value

Yaf_Router

Yaf_Request_Abstract getRequest()

No description

Return Value

Yaf_Request_Abstract

Yaf_Dispatcher setErrorHandler(callable $callback, int $error_types = YAF_ERR_TYPE_ERROR)

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

Yaf_Dispatcher

Yaf_Dispatcher setDefaultModule(string $module)

Change default module name

Parameters

string $module

Return Value

Yaf_Dispatcher

Yaf_Dispatcher setDefaultController(string $controller)

Change default controller name

Parameters

string $controller

Return Value

Yaf_Dispatcher

Yaf_Dispatcher setDefaultAction(string $action)

Change default action name

Parameters

string $action

Return Value

Yaf_Dispatcher

Yaf_Dispatcher returnResponse(bool $flag)

No description

Parameters

bool $flag

Return Value

Yaf_Dispatcher

Yaf_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

Yaf_Dispatcher

Yaf_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

Yaf_Dispatcher

static Yaf_Dispatcher getInstance()

No description

Return Value

Yaf_Dispatcher

Yaf_Response_Abstract dispatch(Yaf_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.

Yaf_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

Yaf_Dispatcher

Yaf_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

Yaf_Dispatcher

Yaf_Dispatcher registerPlugin(Yaf_Plugin_Abstract $plugin)

Register a plugin(see Yaf_Plugin_Abstract). Generally, we register plugins in Bootstrap(see Yaf_Bootstrap_Abstract).

Parameters

Yaf_Plugin_Abstract $plugin

Return Value

Yaf_Dispatcher

Yaf_Dispatcher|null setResponse(Yaf_Response_Abstract $response)

No description

Parameters

Yaf_Response_Abstract $response

Return Value

Yaf_Dispatcher|null