Yii2.0 Visitor Access Restrictions

Source: Internet
Author: User

Recently in the YII2.0 project, which needs to implement a function: No login can not access some of the pages, that is, visitor identity access restrictions. After half a day of information, finally found the answer. The workaround is as follows:

In access, access means that there is a configuration item ' only ' =>[' login ', ' about '] what does that mean, meaning it only works in login, about two action, that is, when the action

is login, about, will enter the rules to do next verification.

But what do we want to do except login, registration, and no other action is allowed to access? There are other configurations, we change only to except, what meaning, is to remove what outside of meaning, is

says it works for an action other than login and signup.  Next, in rules=>[[' action ' =>[' login ', ' signup '], ' Allow ' =>true, ' roles ' =>['? ']] , the rules is the rule, here you can write.

Multiple, actions are the rules for which Action,allow refers to whether to allow access, the ' Roles ' field is critical, which is allowed to access the role. which Represents a visitor, @ represents a logged-on user.


Public function behaviors ()
{return
    [
        ' Access ' => [
            ' class ' => accesscontrol::classname (), '
            except ' =>[' login ', ' signup ', '
            rules ' => ['
                    actions ' => [' login ', ' signup '],
                    ' Allow ' => true,
                    ' roles ' => ['? '],
                ],
            ],
        ],
        ' verbs ' => [
            ' class ' => verbfilter :: ClassName (),
            ' actions ' => [
                ' logout ' => [' post '],
            ],
        ]
    ;
}

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.