How does YII set the same filter for All controllers, instead of copying the filter code to one Controller?

Source: Internet
Author: User
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 yourcontrollerPassedfiltersUsage

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.