How does Yii set the same filter for all controllers, instead of copying the filter code to a single controller?

Source: Internet
Author: User
Tags yii
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')        );    }}
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.