无法从Yii2控制器设置元标记

时间:2015-10-08 05:04:25

标签: php yii2 yii2-advanced-app

在控制器中我有

$this->view = $this->getView();
...
$this->view->registerMetaTag(['name' => 'meta', 'content' => '...']);
$this->view->render();

在视图中<?php $this->head() ?>导致仅插入资源,没有元数据:

<link href="/frontend/web/assets/a8c97299/toolbar.css" rel="stylesheet">
...

页面的其余部分呈现正常。

我试图弄清楚在头部插入期间框架中发生了什么here。似乎$this->clear();禁用了呈现的元数据。我已成功登录

<meta name="meta" content="...">
<link href="/frontend/web/assets/a8c97299/toolbar.css" rel="stylesheet">
...

从那里开始,并且注释$this->clear(); metas按预期呈现在视图中。

如何解释和解决这个问题?

1 个答案:

答案 0 :(得分:2)

你应该在你的控制器中做:

public class Main<T> {

    public void work(T t){
        // Is there a way to get t's fields and values?
    }

}

在布局文件中:

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