G
G
goaf2017-04-05 18:01:04
Yii
goaf, 2017-04-05 18:01:04

Yii2. How to make a redirect to authorization?

Hello!
Faced a very unpleasant problem, all the solutions found did not help.
I created a project based on yii2-advanced (I work in the backend for now), added a couple of modules (including users).
When an unauthorized user enters the module page, he is redirected to the page with authorization. Everything works fine, except when we go to a page like: project.dev/random_string. In this case, a 404 error page is displayed with all the data available to authorized users.
Those. when working correctly, it should first transfer to the page with authorization, and then to the selected page with an error.
I understand that the problem is somewhere in the configs. Those. access control for site and modules works as specified, but accessing a non-existent module causes this problem.
Code from backend\SiteController

public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['login', 'error'],
                        'allow' => true,
                    ],
                    [
                        'actions' => ['logout', 'index'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

Part of the config from backend\main.php
'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
                'login' => 'site/login',
                '' => 'site/index'
            ],
        ],

Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2017-04-05
@webinar

In this case, a 404 error page is displayed with all the data available to authorized users.

Well, change the layout for the page with errors, so that there is nothing superfluous. Making an error page available only to a registered user is a very big mistake. Including because of the possibility to catch a cyclic redirection.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question