引用,致謝
Yaf_Controller_Abstract對(duì)象
abstract Yaf_Controller_Abstract {
/** 參數(shù) **/
protected array actions ;
protected Yaf_Request_Abstract _request ;
protected Yaf_Response_Abstract _response ;
protected Yaf_View_Interface _view ;
protected string _script_path ;
/** 方法 **/
private void __construct ( void );
public void init ( void );
public string getModuleName ( void );
public Yaf_Request_Abstract getRequest ( void );
public Yaf_Response_Abstract getResponse ( void );
public Yaf_View_Interface getView ( void );
public Yaf_View_Interface initView ( void );
public boolean setViewPath ( string $view_directory );
public string getViewPath ( void );
public Yaf_Response_Abstract render ( string $action_name , array $tpl_vars = NULL );
public boolean display ( string $action_name , array $tpl_vars = NULL );
public boolean forward ( string $action , array $invoke_args = NULL );
public boolean forward ( string $controller , string $action , array $invoke_args = NULL );
public boolean forward ( string $module , string $controller , string $action , array $invoke_args = NULL );
public boolean redirect ( string $url );
}
1. actions功能(獨(dú)立action译打,同yii的actions)
1.1 使用場(chǎng)景
- 當(dāng)Controller文件很大時(shí),把a(bǔ)ction拆出去跳仿。
- 多個(gè)Controller調(diào)用一個(gè)action,把a(bǔ)ction提出竿音,用此功能調(diào)用。
1.2 如何使用拴驮?
1.添加actions,key為action名,value為action所在路徑春瞬。路徑的根是application目錄。下面栗子的路徑application/actions/Login.php
<?php
class IndexController extends Yaf_Controller_Abstract{
public $actions = [
"login" => "actions/Login.php"
];
}
2.創(chuàng)建Login.php套啤,添加execute方法
注意class加Action宽气,引入的yafAction類,execute為入口
<?php
class LoginAction extends Yaf_Action_Abstract{
public function execute(){
echo __CLASS__ .":" . __FUNCTION__;
exit;
}
}
2.參數(shù)
- _request潜沦,請(qǐng)求實(shí)例萄涯,屬性值由Yaf_Dispatcher保證,可用getRequest()獲取
- _response, 響應(yīng)實(shí)例唆鸡,屬性值由Yaf_Dispatcher保證涝影,可用getResponse()獲取
- _view,視圖引擎争占,調(diào)用Yaf_Controller_Abstract::getView或Yaf_Controller_Abstract::initView才能使用燃逻,延時(shí)實(shí)例化來(lái)提高性能(因?yàn)閅af采用延遲實(shí)例化視圖引擎的策略, 所以只有在使用前調(diào)用此方法, 視圖引擎才會(huì)被實(shí)例化)
- _script_path序目,視圖文件目錄,屬性值由Yaf_Dispatcher保證唆樊,可通過(guò)Yaf_Controller_Abstract::setViewPath來(lái)改變
3.方法
3.1 getRequest宛琅,獲取請(qǐng)求實(shí)例,Yaf_Request_Abstract
3.2 getResponse逗旁,獲取響應(yīng)實(shí)例嘿辟,Yaf_Response_Abstract
3.3 getView,獲取當(dāng)前視圖引擎片效,Yaf_View_Interface红伦。獲取的就是 _view
3.4 initView,初始化視圖引擎(yaf為性能淀衣,不會(huì)加載昙读。所以要使用前,手動(dòng)加載)膨桥,Yaf_View_Interface蛮浑。??不初始化也可以使用_view,和文檔描述不一致
3.5 setViewPath只嚣,設(shè)置視圖模板根目錄(絕對(duì)路徑)沮稚,模板路徑=根目錄+控制器名+模板名
3.6 getViewPath,獲取視圖模板根目錄(絕對(duì)路徑)
3.7 render册舞,返回渲染結(jié)果蕴掏。輸出需手動(dòng)echo
//效果同$this->display("out",["param1"=>"login out"]);
echo $this->render("out",["param1"=>"login out"]);
3.8 display,直接輸出渲染結(jié)果调鲸,display之后代碼依然執(zhí)行
//指定模板根目錄
$this->setViewPath(APP_PATH."/application/views/Login/");
//模板文件路徑: application/views/Login/{控制器名}/out.phtml
$this->display("out",["param1"=>"login out"]);
//模板文件路徑: application/views/{控制器名}/Login.phtml
$this->display("Login",["param1"=>"hello world"]);
sleep(1);
echo "end";//模板內(nèi)容后盛杰,輸出end
3.9 forward,請(qǐng)求其他action藐石,比如未登錄跳轉(zhuǎn)到登錄頁(yè)即供。不會(huì)立即跳轉(zhuǎn),當(dāng)天action處理完才會(huì)調(diào)到下一個(gè)于微,module募狂、controller、action的入?yún)⒃O(shè)計(jì)很人性角雷,按數(shù)量從小到大排列
$this->forward("Login","out");
sleep(1);
echo "hello world";//先執(zhí)行sleep和echo祸穷,然后調(diào)用Login控制器的out方法,輸出帶有hello world
3.10 redirect勺三,重定向到新地址雷滚。action代碼執(zhí)行完成后再跳轉(zhuǎn)。多個(gè)redirect吗坚,最后一個(gè)為結(jié)果
$this->redirect("/Login/");//跳轉(zhuǎn)到Login控制器祈远,執(zhí)行indexAction呆万。也可指定action,如/Login/out
$this->redirect("http://www.baidu.com");跳轉(zhuǎn)到外部地址