Fatal Error Html::csrfMetaTags() When Updating the Layout File
In chapter 3 (around pages 72-73) we update the layout file to use some assets to make our pages look better, but this can result in a fatal error:
PHP Fatal Error – yii\base\ErrorException Call to undefined method yii\helpers\Html::csrfMetaTags()
This post is specific to the 'Web Application Development with Yii 2 and PHP' book
Whilst I'm not totally sure of the issue here, it seems to be related to a composer update for me. As of today, using the latest composer version it is not possible to install a yii basic or advanced project due to errors in bower-asset versions required by yii2.
First of all you need to make sure you have the asset plugin installed globally for composer which you add by running the following command:
composer global require "fxp/composer-asset-plugin:1.0.0-beta4"
You need this regardless of your composer version.
Next, to solve my errors I needed to rollback my installed version of composer:
composer self-update --rollback
Which showed me "Rolling back to version 2014-10-20_19-16-14-a309e1d"
At this point I had already deleted my vendor folder and composer.lock file due to some other posts I found on github so I ran the install again (after moving to my app directory):
composer install
This time the bower assets installed correctly and my errors were gone.
At this point my composer.json file looks like this:
{
"minimum-stability": "stable",
"require": {
"php": ">=5.4.0",
"codeception/codeception": "*",
"fzaninotto/faker": "*",
"yiisoft/yii2": "*",
"netwing/selenium-server-standalone": "2.39.0",
"yiisoft/yii2-gii": "*",
"yiisoft/yii2-debug": "*",
"yiisoft/yii2-bootstrap": "*",
"yiisoft/yii2-swiftmailer": "*"
},
"extra": {
"asset-installer-paths": {
"npm-asset-library": "vendor/npm",
"bower-asset-library": "vendor/bower"
}
}
}