1) create a BaseController to inherit the Controller (all operations on the background must inherit the BaseController ):
Add the following in BaseController:
Copy codeThe Code is as follows:
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' => 'the user has not logged on. ', 'Callback' => 'window. location = "'. $ url .'";'));
} Else if (Yii: app ()-> request-> isAjaxRequest ){
Echo '<script language = "javascript"> window. location = "'. $ url. '"; </script> ';
} Else {
$ This-> redirect ($ url );
}
Exit;
}
Return true;
}
Create the Authority. php file in the components directory:
Copy codeThe Code is as follows:
<? Php
/**
* Permission check component
*/
Class Authority extends CComponent {
Private $ NOTLOGIN =-1;
Private $ FAILED =-2;
Private $ PASS = 1;
Public function init (){
}
/**
* Check for Logon
* @ Return boolean
*/
Function isLogin (){
Return isset (Yii: app ()-> session ['user'])? $ This-> PASS: $ this-> NOTLOGIN;
}
/**
* Get status value
* @ Param string $ name
* @ Return int
*/
Public function getStatus ($ name ){
Return $ this-> $ name;
}
}