A
A
Andrey Tokmakov2016-04-06 17:59:42
Yii
Andrey Tokmakov, 2016-04-06 17:59:42

Is it correct to use the standard Menu widget in yii2 like this?

I decided to get rid of the bootstrap menu widgets and use a simple yii\widgets\Menu
When I got to the Logout button, I first encountered a problem - items does not accept the values ​​that were written for Nav / NavBar:

$menuItems[] = '<li>'
            . Html::beginForm(['/site/logout'], 'post')
            . Html::submitButton(
                'Logout (' . Yii::$app->user->identity->username . ')',
                ['class' => 'btn btn-link']
            )
            . Html::endForm()
            . '</li>';

Decided to do it right:
$menuItems[] = ['label' => 'Выйти('.Yii::$app->user->identity->username.')', 'url' => '/site/logout'];

As a result of what followed - Method Not Allowed (# 405), the case is clear logout is waiting for POST.
I started to google and, surprisingly, there was not 1 adequate solution, only a proposal to change logout so that it would not ask for POST and a proposal to write my own widget with a bunch of scribbling, inherited from Menu.
After reading the documentation on Menu, it turned out:
$menuItems[] = ['label' => Html::beginForm(['/site/logout'], 'post')
            . Html::submitButton('Logout ('. Yii::$app->user->identity->username . ')'), 'encode' => false];

Actually, the whole point is 'encode' => false, disabling encode allows you to display the form through the label, and not the code as text.
Google put me in a stupor by the lack of such a simple solution, which made me doubt whether it is right to do this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Deniska Petrov, 2016-04-06
@NPC

[
    'label' => 'Выход',
    'url' => ['/site/logout'],
     'template' => '<a href="{url}" data-method="post">{label}</a>'
],

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question