简介
dispatcher在router之后,负责调用控制器,返回响应数据
Yaf_Dispatcher对象
final Yaf_Dispatcher {
/** 属性 **/
protected static Yaf_Dispatcher _instance ;
protected Yaf_Router_Interface _router ;
protected Yaf_View_Abstract _view ;
protected Yaf_Request_Abstract _request ;
protected array _plugins ;
protected boolean _render ;
protected boolean _return_response = FALSE ;
protected boolean _instantly_flush = FALSE ;
protected string _default_module ;
protected string _default_controller ;
protected string _default_action ;
/** 方法 **/
public static Yaf_Dispatcher getInstance ( void );
public Yaf_Dispatcher disableView ( void );
public Yaf_Dispatcher enableView ( void );
public boolean autoRender ( bool $flag );
public Yaf_Dispatcher returnResponse ( boolean $flag );
public Yaf_Dispatcher flushInstantly ( boolean $flag );
public Yaf_Dispatcher setErrorHandler ( mixed $callback ,int $error_type = E_ALL | E_STRICT );
public Yaf_Application getApplication ( void );
public Yaf_Request_Abstract getRequest ( void );
public Yaf_Router_Interface getRouter ( void );
public Yaf_Dispatcher registerPlugin ( Yaf_Plugin_Abstract $plugin );
public Boolean setAppDirectory ( string $directory );
public Yaf_Dispatcher setRequest ( Yaf_Request_Abstract $request );
public Yaf_View_Interface initView ( void );
public Yaf_Dispatcher setView ( Yaf_View_Interface $view );
public Yaf_Dispatcher setDefaultModule ( string $default_module_name );
public Yaf_Dispatcher setDefaultController ( string $default_controller_name );
public Yaf_Dispatcher setDefaultAction ( string $default_action_name );
public Yaf_Dispatcher throwException ( boolean $switch = FALSE );
public Yaf_Dispatcher catchException ( boolean $switch = FALSE );
public Yaf_Response_Abstract dispatch ( Yaf_Request_Abstract $request );
}
1. 模板类
1.1 disableView,关闭自动加载模板(默认开启),开启状态,每个action结束会根据action名加载模板文件
Yaf_Dispatcher::getInstance()->disableView();
1.2 enableView,开启自动加载模板(默认开启)
Yaf_Dispatcher::getInstance()->disableView();
1.3 autoRender,disableView与enableView合体版
Yaf_Dispatcher::getInstance()->autoRender(TRUE);//同enableView
Yaf_Dispatcher::getInstance()->autoRender(FALSE);//同disableView
1.4 flushInstantly,是否自动响应,在enableView下开启,会调用Yaf_Controller_Abstract::display方法。直接输出响应给请求端
/* 立即输出响应 */
Yaf_Dispatcher::getInstance()->flushInstantly(TRUE);
/* 此时会调用Yaf_Controller_Abstract::display方法 */
$application->run();
1.5 initView,初始化模板引擎,自定义模板引擎要先setView在initView
1.6 setView,设置模板引擎
2.异常类
2.1 setErrorHandler,自定义错误处理函数,throwException关闭时,产生的错误会调用
Yaf_Dispatcher::getInstance()->setErrorHandler("myErrorHandler");
2.2 throwException,设置yaf出错是否抛出异常
//不抛出异常
Yaf_Dispatcher::getInstance()->throwException(FALSE);
2.3 catchException,是否捕获异常
Yaf_Dispatcher::getInstance()->catchException(TRUE);
3.响应类
3.1 returnResponse,不自动返回response对象。默认自动返回
/* 关闭自动响应, 交给rd自己输出*/
$response =
$application->getDispatcher()->returnResponse(TRUE)->getApplication()->run();
/** 输出响应*/
$response->response();
4.请求类
4.1 getRequest,获取请求实例
Yaf_Dispatcher::getInstance()->getRequest();
4.2 setRequest,设置请求,??不知道用处
$request = new Yaf_Request_Simple("Index", "Index", "index");
Yaf_Dispatcher::getInstance()->setRequest($request);
5.应用类
5.1 getApplication,获取应用实例
$application = Yaf_Dispatcher::getInstance()->getApplication();
//效果同上,明显更短
$application = Application::app();
5.2 setAppDirectory,新版本好像没了
6.其他
6.1 getRouter,获取路由实例
Yaf_Dispatcher::getInstance()->getRouter();
6.2 registerPlugin,注册自定义插件类
Yaf_Dispatcher::getInstance()->registerPlugin(Yaf_Plugin_Abstract $user);