Method Not Allowed (#405)

I all i start with a fresh yii2 basic application.

I want to install a new theme: Material Default

I create a new directory : <myapp>/web/themes/material and i put there the css,font,js,and layouts directory. Layout dir, contains the new main.php

Then i modify web.php as follow:


        'view' => [

            'class' => 'yii\web\View',

            'theme' => [

                'class' => 'yii\base\Theme',

                'basePath' => '@web/themes/material',

                'baseUrl'   => '@web/themes/material',

                'pathMap' => ['@app/views' => '@webroot/themes/material'],

            ]

        ],

My site now has the new theme, but if i try to perform the action: site/logout i have the error: Method Not Allowed (#405)

I read other post where suggest to change the VerbFilter settings or to add a data-method="post" in the link but i ask myself: why i must change these if they work fine with the other theme (default)?

So i read that could be the AssetBundle. In effect in my new main.php there is no asset registred. So i copy from the default main.php the follow code:


use app\assets\AppAsset;

AppAsset::register($this);



but i didn’t solve yet.

So i try to edit the assets\Appasset removing the $depends but nothing to do. I have always the error #405.

Can anyone help me? Thank’s

The main.php (layout) of that theme seems not to be 100% compatible with that of the default main.php.

The correction should be done on the theme’s main.php. Do not touch other parts of your app.

  1. As you have noticed, it lacks the following line:



AppAsset::register($this);



  1. "Logout" link is missing.

You have to write something similar that you see in the default main.php.

  1. Also, the following line is missing in <header> section:



    <?= Html::csrfMetaTags() ?>



The absence of this line will result in “Bad Request (400)” error when you hit “logout” link, even when you have the correct option of ‘data-method=“post”’.

hi softark, thank’s for the help, with that, now i think to solve my problem.

  1. I add in the head of my new main.php the line

<?= Html::csrfMetaTags() ?>

  1. I substitute the Menu Widget used with a NavBar Widget because i don’t know how to pass the data-method option to the tag <a> in the Menu Widget:

OLD:


  <nav class="orange darken-1" role="navigation">

    <div class="container">

      <div class="nav-wrapper"><a id="logo-container" href="#" class="brand-logo"><?php echo Html::encode(\Yii::$app->name); ?></a>

	  		<?php

						echo Menu::widget([

						    'options' => ['id' => "nav-mobile", 'class' => 'right side-nav'],

						    'items' => [

						        ['label' => 'Home', 'url' => ['site/index']],

						        ['label' => 'About', 'url' => ['site/about']],

						        ['label' => 'Contact', 'url' => ['site/contact']],

						        //['label' => 'Login', 'url' => ['site/login'], 'visible' => Yii::$app->user->isGuest],

                                Yii::$app->user->isGuest ? (

                                  ['label' => 'Login', 'url' => ['/user/login']]

                                ) : (

                                  ['label' => 'Logout', 'url' => ['/user/logout'] , 'options' => ['data-method' => 'post']] //this is a test, but i don't know how to pass this option to the <a> tag with this widget

                                )

						    ],

						]);

					?>

          <a href="#" data-activates="nav-mobile" class="button-collapse"><i class="mdi-navigation-menu"></i></a>

      </div>

    </div>

  </nav>

NEW:


    <nav class="orange darken-1" role="navigation">

    <div class="container">

      <div class="nav-wrapper"><a id="logo-container" href="#" class="brand-logo"><?php echo Html::encode(\Yii::$app->name); ?></a>

        <?php

            $menuItems = [

                ['label' => 'Home', 'url' => ['/site/index']],

                ['label' => 'About', 'url' => ['/site/about']],

                ['label' => 'Contact', 'url' => ['/site/contact']],

            ];

            if (Yii::$app->user->isGuest) {

              $menuItems[] = ['label' => 'Register', 'url' => ['/user/register']];

              $menuItems[] = ['label' => 'Login', 'url' => ['/user/login']];

            } else {

              $menuItems[] = [

                  'label' => 'Logout (' . Yii::$app->user->identity->username . ')',

                  'url' => ['/user/logout'],

                  'linkOptions' => ['data-method' => 'post']

              ];

            }

            echo Nav::widget([

                'options' => [

                    'class' => 'right side-nav',

                    'id' => 'nav-mobile'

                ],

                'items' => $menuItems,

            ]);

        ?>

        <a href="#" data-activates="nav-mobile" class="button-collapse"><i class="mdi-navigation-menu"></i></a>

      </div>

    </div>

  </nav>



Thank you!