YII中的过滤器的细节

<?php
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
*/

class SessionCheckFilter extends CFilter {
    public function init() {
        parent::init();
        $this->attachBehaviors(array(
            'class' => 'ext.behavior.JsonBehavior'
        ));
    }
    protected function preFilter($filterChain) {
        if (isset(Yii::app()->user->userId) && (!empty(Yii::app()->user->userId))) {

             return true;
        } else {
            _echo(ERROR_SESSION, '用户id获取失败,您需要重新登录');
           Yii::app()->user->loginRequired();
            return false;
        }


    }


}

为了简便,你可能会这样写:

<?php
/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */

class Controller extends CController {
    public $layout = '//layouts/column1';
    public $menu = array();
    public $breadcrumbs = array();
    public $error;
    public $jsonText;
    public function behaviors() {

        return array(
            'class' => 'ext.behavior.JsonBehavior'
        );
    }
     public function filters() {
        return array(
            array('application.filters.SessionCheckFilter - user/default/login,user/default/logout,user/default/registration,temp/default/index'),
        );
    }
    protected function beforeAction(CAction $action) {
        $this->error = new ErrorCapture();

        return true;
    }
    public function initParams($className = '', $type = 'model') {
        $model = null;
        if ($type == 'model') {
            $model = $className::model();
        } else {
            $model = new $className();
        }
        if (isset($_POST[get_class($model) ])) {
            $model->attributes = $_POST[get_class($model)];
        }

        return $model;
    }
    /**
     * 手机客户端的分页,防止分页重复,与网页显示无关
     * @param  CActiveDataProvider $dataProvider 
     * 
     */
    public function page(CActiveDataProvider & $dataProvider) {
        $pageCount = $dataProvider->getPagination()->getPageCount();

        if ($dataProvider->getTotalItemCount() == 0) {

            $this->send(ERROR_EMPTY, 'data empty');
        }
       else if (isset($_GET['page'])) {
            if ($pageCount >= $_GET['page']) {
                $this->send(0, $dataProvider->getData());
            }
        }
        else{
            $this->send(ERROR_NONE, $dataProvider->getData());
        }

    }
}

这样就不会在子controller里面写了。但是实际上这不行。这样在父类中写过滤器并不能完成过滤、估计是YII的bug

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值