如何在Yii 2中使用registerMetatag

Chi*_*ing 18 yii2

我正试图通过使用在页眉上呈现元标记:在我的视图中:

<?php
$this->registerMetaTag(['name' => 'keywords', 'content' => 'yii, framework, php']);
?>
Run Code Online (Sandbox Code Playgroud)

在我的布局中,我将此代码放在head标记位置:

<?php $this->head()?>
Run Code Online (Sandbox Code Playgroud)

但它不起作用.任何有同样问题的人都可以帮我解决这个问题.谢谢 !!

Ser*_*gey 29

要通过该registerMetaTag方法定义一些元标记,请在控制器中使用以下代码:

public function actionIndex()
{
    \Yii::$app->view->registerMetaTag([
        'name' => 'description',
        'content' => 'Description of the page...'
    ]);
    return $this->render('index');
}
Run Code Online (Sandbox Code Playgroud)

和布局中的这个标记:

<head>
    <?php $this->head() ?>
</head>
Run Code Online (Sandbox Code Playgroud)

我已经更新了可以在这里下载的基本演示应用程序(来自http://www.yiiframework.com/download/) - https://www.dropbox.com/s/9rarzhtw65e5zo1/basic.zip?dl=0并看到这种方法在行动.