Yii2, how to replace the meta tag for the meta tags from the "view" with the default meta tags in the "layout"

I want to put the default metadata description tag for the main layout of the template.

There are many cases where I needed to replace the default meta tags, such as description and keywords, with data customization in the views.

I tried:

$this->registerMetaTag

In the views where I need to customize the meta tags, and also I used this in the main layout.

I thought that if I use meta tags with their identifier or name, it will be replaced, but as a result, repeated meta tags will be shown, for example,

<meta id="main_index" name="Description" content="my default content added by layout.">


<meta id="main_index" name="Description" content="my customized content added by the view"></head>

What is the best way to provide a default description tag, but it can be customized using views when I call $this->registerMetaTag

+4
source share
3

, , , , , registerMetaTag .

  • ,

    class MyController extends Controller
    {    
        public function beforeAction($event){
        $this->view->title = Yii::$app->params['pageTitle'];
    
        \Yii::$app->view->registerMetaTag(Yii::$app->params['pageDefaultDescription'],"default_description");
        \Yii::$app->view->registerMetaTag(Yii::$app->params['pageDefaultKeywords'],"default_keywords");
        \Yii::$app->view->registerMetaTag(Yii::$app->params['pageDefaultAuthor'],"default_author");
    
        // Open Graph Tags
        \Yii::$app->view->registerMetaTag(Yii::$app->params['pageDefaultOG_Description'],"default_og_description");
        \Yii::$app->view->registerMetaTag(Yii::$app->params['pageDefaultOG_SiteName'],"default_og_sitename");
        \Yii::$app->view->registerMetaTag(Yii::$app->params['pageDefaultOG_Title'],"default_og_title");
        \Yii::$app->view->registerMetaTag(Yii::$app->params['pageDefaultOG_Type'],"default_og_type");
    
    
        return parent::beforeAction($event);
        }
    } 
    
  • , author .. param .

  • , , MyController, .

  • , .

     public function actionIndex()
    {
    
    
        \Yii::$app->view->registerMetaTag([
            'name' => 'description',
            'content' => 'my customize description for the index.',
            'id'=>"main_index"
        ],"main_index"); //this will now replace the default one.
    
    
        return $this->render('index');
    }
    
+5

. , , .

/params.php

<?php
return [
    'adminEmail' => 'admin@example.com',
    'og_title' => ['property' => 'og:title', 'content' => 'title'],
    'og_description' => ['property' => 'og:description', 'content' => 'description'],
    'og_url' => ['property' => 'og:url', 'content' => '/'],
    'og_image' => ['property' => 'og:image', 'content' => 'image']
];

//main.php

<?php
    $this->registerMetaTag(Yii::$app->params['og_title'], 'og_title');
    $this->registerMetaTag(Yii::$app->params['og_description'], 'og_description');
    $this->registerMetaTag(Yii::$app->params['og_url'], 'og_url');
    $this->registerMetaTag(Yii::$app->params['og_image'], 'og_image');
?>

, ,

//index.php

<?php
    Yii::$app->params['og_title']['content'] = 'custom title';
    Yii::$app->params['og_description']['content'] = 'custom desc';
    Yii::$app->params['og_url']['content'] = '/new/url';
    Yii::$app->params['og_image']['content'] = 'image.jpg';
?>
+4

You can manage a registered meta tag using a similar identifier. Make sure meta tags are created using Yii. From your question, you have already created a metric tag that cannot be replaced because Yii does not control it.

public function actionTest1{
    $this->registerMetaTag('your example', 'description', null, array(), 'metaDescription');
    $this->render('myView');
}

public function actionTest2{
    $this->registerMetaTag('your example2', 'description', null, array(), 'metaDescription');
    $this->render('myView2');
}
+1
source

All Articles