phpバックグラウンドでは、ユーザーがメソッドインスタンスに直接アクセスすることを回避する方法

1953 ワード

1)BaseControllerコントローラ継承Controllerを作成する(バックグラウンドのすべての操作はBaseControllerを継承する):
BaseControllerに追加:
 
  
public function checkLogin() {

        if (Yii::app()->authority->isLogin() == Yii::app()->authority->getStatus('NOTLOGIN')) {
            $url = $this->createUrl('user/login');
            if (Yii::app()->request->isPostRequest && Yii::app()->request->isAjaxRequest) {
                echo json_encode(array('code' => -101, 'message' => ' 。', 'callback' => 'window.location="' . $url . '";'));
            } else if (Yii::app()->request->isAjaxRequest) {
                echo 'window.location="' . $url . '";';
            } else {
                $this->redirect($url);
            }
            exit;
        }
        return true;
    }

componentsディレクトリの下にAuthorityを作成します.phpファイル:
 
  

/**
 *
 */
class Authority extends CComponent {
    private $NOTLOGIN = -1;
    private $FAILED = -2;
    private $PASS = 1;

    public function init() {

    }

    /**
     *
     * @return boolean 
     */
    function isLogin() {
        return isset(Yii::app()->session['user']) ? $this->PASS : $this->NOTLOGIN;
    }

  
    /**
     *
     * @param string $name
     * @return int 
     */
    public function getStatus($name){
        return $this->$name;
    }
}