First 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, '用户id获取失败,您需要重新登录'); $this->redirect(array('user/login')); } } public function filters() { return array( 'IsSessionWrong - login,register', ); }
Because the client is the phone. Mobile Access is accessed through the session. When the session does not exist, tell the client to log in again (the default is 10 days).
But I do this only in the middle of the Usercontroller (except registration and login do not need to judge) to filter. In fact, all controllers have to be tested. If I have to test it all, I have to copy this piece of code to all controllers. Is there a good way to set this judgment session for all controllers to play a role?
Reply content:
First 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, '用户id获取失败,您需要重新登录'); $this->redirect(array('user/login')); } } public function filters() { return array( 'IsSessionWrong - login,register', ); }
Because the client is the phone. Mobile Access is accessed through the session. When the session does not exist, tell the client to log in again (the default is 10 days).
But I do this only in the middle of the Usercontroller (except registration and login do not need to judge) to filter. In fact, all controllers have to be tested. If I have to test it all, I have to copy this piece of code to all controllers. Is there a good way to set this judgment session for all controllers to play a role?
You can define a filter to inherit from CFilter
,
class SessionCheckFilter extends CFilter { protected function preFilter($filterChain) { if (isset(Yii::app()->user->userId) && (!empty(Yii::app()->user->userId))) { $filterChain->run(); } else { _echo(2, '用户id获取失败,您需要重新登录'); $this->redirect(array('user/login')); } }}
And then use the controller
method in filters
your
class UserController extends Controller { public function filters() { return array( array('application.filters.SessionCheckFilter - edit, create') ); }}