本文實(shí)例講述了Yii控制器中filter過濾器用法。分享給大家供大家參考,具體如下:
指定過濾動作,(如下projectContext()方法在新建,列表,管理頁面調(diào)用時(shí)使用)
public function filters()
{
return array(
'accessControl', // perform access control for CRUD operations
'postOnly + delete', // we only allow deletion via POST request
'projectContext +create index admin',
);
}
添加一些過濾器邏輯,定義過濾方法,過濾方法前必加filter
public function filterProjectContext($filterChain)
{
$prijectId = null;
if(isset($_POST['pid'])){
$projectId = $_POST['pid'];
}elseif(isset($_GET['pid'])){
$projectId = $_GET['pid'];
}
$this->loadProject($projectId);
$filterChain->run();
}
public function loadProject($projectid)
{
if($this->_project === null){
$this->_project = Project::model()->findbyPK($projectid);
if($this->_project === null){
throw new CHttpException(404,'請求和項(xiàng)目沒找到!');
}
}
return $this->_project;
}
希望本文所述對大家基于Yii框架的PHP程序設(shè)計(jì)有所幫助。
2025國考·省考課程試聽報(bào)名