加载中...

Yii-控制器- filter过滤器实现步骤


指定过滤动作,(如下projectContext()方法在新建,列表,管理页面调用时使用)
  1. public function filters()
  2. {
  3. return array(
  4. 'accessControl', // perform access control for CRUD operations
  5. 'postOnly + delete', // we only allow deletion via POST request
  6. 'projectContext +create index admin',
  7. );
  8. }
添加一些过滤器逻辑,定义过滤方法,过滤方法前必加filter
  1. public function filterProjectContext($filterChain)
  2. {
  3. $prijectId = null;
  4. if(isset($_POST['pid'])){
  5. $projectId = $_POST['pid'];
  6. }elseif(isset($_GET['pid'])){
  7. $projectId = $_GET['pid'];
  8. }
  9. $this->loadProject($projectId);
  10. $filterChain->run();
  11. }
  12.  
  13. public function loadProject($projectid)
  14. {
  15. if($this->_project === null){
  16. $this->_project = Project::model()->findbyPK($projectid);
  17. if($this->_project === null){
  18. throw new CHttpException(404,'请求和项目没找到!');
  19. }
  20. }
  21. return $this->_project;
  22. }



还没有评论.