C
C
connor742016-05-30 18:54:24
Yii
connor74, 2016-05-30 18:54:24

Why is logout not working in Yii2?

Good evening!
I understand that the voros is hackneyed, I looked at the search on the toaster: Yii2 does not work data-method=post? - did not find the answer.
Bottom line:
There is a logout link:

<?= Html::a("Выход", 'site/logout', [
                                'data' => [
                                    'method' => 'post'
                                ],
                                ['class' => 'white text-center']
                            ]
                        );?>

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

Well, in AppAsset.php:
class AppAsset extends AssetBundle
{
    public $basePath = '@webroot';
    public $baseUrl = '@web';
    public $css = [
        'css/site.css',
        'css/sticky-footer-navbar.css',
        'css/main.css',
        'css/fstyle.css',
    ];
    public $js = [
        'js/main.js'
    ];
    public $depends = [
        'yii\web\YiiAsset',
        'yii\bootstrap\BootstrapAsset',
    ];
}

Everything seems to be according to science, but the output does not work.
Site on Yii2, basic.
logout code:
public function actionLogout()
    {
        Yii::$app->user->logout();

        return $this->goHome();
    }

In config:
'user' => [
            'identityClass' => 'app\models\User',
            'enableAutoLogin' => true,
        ],

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander N++, 2016-05-30
@sanchezzzhak

I suppose you are breaking on get request to logout, and you only allow post requests there yii2 should respond with 405 response status (if I guessed correctly)
Remove or add

'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['get','post'],
                ],
            ],

C
connor74, 2016-05-30
@connor74

solved a problem.

<?= Html::a("Выход", ['site/logout'], [
                                'data' => [
                                    'method' => 'post'
                                ],
                                ['class' => 'white text-center']
                            ]
                        );?>

added square brackets ['site/logout'] and it worked... how so?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question