Right place for registerMetaTag()


I use Yii2 to generate Web-Application sites. Now, I need to set a global Meta-Tag to each website that is generated by Yii2.
So the question is: what is the right place for registerMetaTag() to reach all pages and to avoid duplicated calls.

It looks like the controller is not the right place, because I have many controllers and I don’t like to define that meta-tag in each controller again and again.

Also the main-layout view is not the right place, because, the layout should be interchangeable. So I need to hold the changes in views/layouts/main.php (etc) small as possible. Otherwise I have to apply all additional Yii stuff to new layout-templates.

Next option: the web/index.php, which looks very basic, not the right place for application-specific code, isn’t it?

So where is the best location to place an global metaTag?

Thank you.

1 Like

I think meta tag for all pages should be placed in layout.


I tend to agree with DeryabinSergey

checkout https://github.com/daxslab/yii2-taggedview, perhaps it fits directly what you want to do or you can take the idea.

Main layout with shared partial view (extract one-two lines into separate partial view, if it’s worth to you).

But note - separate view/file = extra I/O operation if you want to be performance freak :slight_smile:

I usually place meta tags (e.g. google analytics) into Yii::$app->params - and then it from any view/layout, usually applies only to frontend layout(s).

1 Like