First, let's look at the code: {code...} because the client is a mobile phone. Mobile Access is accessed through session. When this session does not exist, the client is told to log on again (10 days by default ). But I can only do this in the middle of UserController (except registration and login... first, let's look at the Code:
Class UserController extends Controller {public function filterIsSessionWrong ($ filterChain) {if (isset (Yii: app ()-> user-> userId )&&(! Empty (Yii: app ()-> user-> userId) {$ filterChain-> run ();} else {_ echo (2, 'user ID acquisition failed, you need to log on again '); $ this-> redirect (array ('user/login');} public function filters () {return array ('issessionwrong-login, register ',);}
Because the client is a mobile phone. Mobile Access is accessed through session. When this session does not exist, the client is told to log on again (10 days by default ).
However, in this way, I can only filter out UserController (except registration and logon, which does not need to be judged. In fact, all controllers need to be tested. If you want to verify the code, I have to copy the code to all controllers. Is there a good way to set this session to all controllers?
Reply content:
First, let's look at the Code:
Class UserController extends Controller {public function filterIsSessionWrong ($ filterChain) {if (isset (Yii: app ()-> user-> userId )&&(! Empty (Yii: app ()-> user-> userId) {$ filterChain-> run ();} else {_ echo (2, 'user ID acquisition failed, you need to log on again '); $ this-> redirect (array ('user/login');} public function filters () {return array ('issessionwrong-login, register ',);}
Because the client is a mobile phone. Mobile Access is accessed through session. When this session does not exist, the client is told to log on again (10 days by default ).
However, in this way, I can only filter out UserController (except registration and logon, which does not need to be judged. In fact, all controllers need to be tested. If you want to verify the code, I have to copy the code to all controllers. Is there a good way to set this session to all controllers?
You can define a filter to inherit fromCFilter
,
Class SessionCheckFilter extends CFilter {protected function preFilter ($ filterChain) {if (isset (Yii: app ()-> user-> userId )&&(! Empty (Yii: app ()-> user-> userId) {$ filterChain-> run ();} else {_ echo (2, 'user ID acquisition failed, you need to log on again '); $ this-> redirect (array ('user/login '));}}}
Then in yourcontroller
Passedfilters
Usage
class UserController extends Controller { public function filters() { return array( array('application.filters.SessionCheckFilter - edit, create') ); }}