title: Adding SEO tags for each page in Yii2
body: <p>I’m very new to this topic. I’ve a site written with PHP in Yii framework. In my customize CDN, where I inject my content through it, my developer designed a section that I can assign different Meta tags or SEO tags to various pages. My issue is that that mechanism does not work and, in fact, when I put, say Meta description, it appears to all pages same. I can’t access to my developer to correct this issue. In below, I’ve provided the codes for layout and Metadata.php.
  As I mentioned, I’m new to this topic and don’t know which codes I should submit for considering. If more codes are needed, please ask me.</p>
  
  <p>My goal is to correct my backend entry (Metadata list section) such that each page has unique Meta tags provided by me. </p>
  
  <pre><code>$metadata = \common\models\base\Metadata::find()-&gt;where(['page_routing' =&gt; 
  Yii::$app-&gt;controller-&gt;id."/". Yii::$app-&gt;controller-&gt;action-&gt;id ])-&gt;one();
  if ( empty($metadata ) )
  $metadata = \common\models\base\Metadata::find()-&gt;where(['id' =&gt; 1 ])- 
  &gt;one();
  ?&gt;
  
  &lt;?php $this-&gt;beginPage() ?&gt;
  
  &lt;!DOCTYPE html&gt;
  
  &lt;html lang="&lt;?= Yii::$app-&gt;language ?&gt;"&gt;
  
  &lt;head&gt;
  &lt;meta charset="&lt;?= Yii::$app-&gt;charset ?&gt;"&gt;
  &lt;?php echo Html::csrfMetaTags() ?&gt;
  &lt;meta name="viewport" content="width=device-width, initial-scale=1"&gt;
  &lt;meta http-equiv="X-UA-Compatible" content="IE=edge"/&gt;
  &lt;meta name="viewport" content="width=device-width, initial-scale=1, 
  maximum-scale=1, user-scalable=no"/&gt;
  &lt;meta name="robots" content="&lt;?=$metadata-&gt;meta_robots?&gt;"/&gt;
  &lt;link rel="canonical" href="https://physexams.com/&lt;?=Yii::$app-&gt;controller- 
  &gt;id."/". Yii::$app-&gt;controller-&gt;action-&gt;id?&gt;" /&gt;
  &lt;meta property="og:image" content="&lt;?=$metadata-&gt;og_image?&gt;"/&gt;
  &lt;meta property="og:title" content="&lt;?=$metadata-&gt;og_title?&gt;â€Œ"/&gt;
  &lt;meta property="og:description" content="&lt;?=$metadata-&gt;og_description?&gt;"/&gt;
  &lt;meta name="twitter:image" content="&lt;?=$metadata-&gt;twitter_image?&gt;"/&gt;
  &lt;meta property="og:locale" content="en_CA" /&gt;
  &lt;meta name="googlebot" content="index" /&gt;
  &lt;meta name="revisit-after" content="1 Days" /&gt;
  &lt;meta name="doc-class" content="Living Document" /&gt;
  &lt;meta name="language" content="english"&gt;
  &lt;?= Html::csrfMetaTags() ?&gt;
  &lt;link rel="publisher" href="&lt;?=$metadata-&gt;publisher?&gt;" /&gt;
  &lt;title&gt;&lt;?= Html::encode($this-&gt;title) ?&gt;&lt;/title&gt;
  
  &lt;?php $this-&gt;head() ?&gt;
  </code></pre>
  
  <p></p>
  
  <p></p>
  
  <p>In above, for being short, I cut other unneeded meta tags and submitted part of the code. If the whole code is necessary, I could submit it. 
  AND the Codes for Metadata.php
      
  
  <pre><code>namespace common\models\base;
  
  use Yii;
  
  /**
   * This is the base-model class for table "metadata".
   *
   * @property integer $id
   * @property string $page_routing
   * @property string $title
   * @property string $meta_description
   * @property string $author
   * @property string $author_link
   * @property string $publisher
   * @property string $og_title
   * @property string $og_type
   * @property string $og_image
   * @property string $og_url
   * @property string $og_description
   * @property string $twitter_card
   * @property string $twitter_url
   * @property string $twitter_title
   * @property string $twitter_description
   * @property string $twitter_image
   * @property string $meta_keywords
   * @property string $meta_robots
   * @property string $meta_copyright
   * @property string $itemprop
   * @property string $Search_Display
   */
   class Metadata extends \yii\db\ActiveRecord
   {
  
  
  
  /**
   * @inheritdoc
   */
  public static function tableName()
  {
      return 'metadata';
  }
  
  /**
   * @inheritdoc
   */
  public function rules()
  {
      return [
          [['meta_description', 'meta_keywords', 'Search_Display'], 
  'string'],
          [['page_routing', 'title', 'author', 'author_link', 'publisher', 
  'og_title', 'og_type', 'og_image', 'og_url', 'og_description', 
  'twitter_url', 'twitter_title', 'twitter_description', 'twitter_image', 
  'meta_robots', 'meta_copyright'], 'string', 'max' =&gt; 1000],
          [['twitter_card'], 'string', 'max' =&gt; 255],
          [['itemprop'], 'string', 'max' =&gt; 2000]
      ];
  }
  
  /**
   * @inheritdoc
   */
  public function attributeLabels()
  {
      return [
          'id' =&gt; 'ID',
          'page_routing' =&gt; 'Page Routing',
          'title' =&gt; 'Title',
          'meta_description' =&gt; 'Meta Description',
          'author' =&gt; 'Author',
          'author_link' =&gt; 'Author Link',
          'publisher' =&gt; 'Publisher',
          'og_title' =&gt; 'Og Title',
          'og_type' =&gt; 'Og Type',
          'og_image' =&gt; 'Og Image',
          'og_url' =&gt; 'Og Url',
          'og_description' =&gt; 'Og Description',
          'twitter_card' =&gt; 'Twitter Card',
          'twitter_url' =&gt; 'Twitter Url',
          'twitter_title' =&gt; 'Twitter Title',
          'twitter_description' =&gt; 'Twitter Description',
          'twitter_image' =&gt; 'Twitter Image',
          'meta_keywords' =&gt; 'Meta Keywords',
          'meta_robots' =&gt; 'Meta Robots',
          'meta_copyright' =&gt; 'Meta Copyright',
          'itemprop' =&gt; 'Itemprop',
          'Search_Display' =&gt; 'Search  Display',
      ];
  }
  }
  </code></pre>
  
  <p>The Codes for Main.php of config: </p>
  
  <pre><code>return [
  'id' =&gt; 'app-frontend',
  'basePath' =&gt; dirname(__DIR__),
  'bootstrap' =&gt; ['log', 'thumbnail'],
  'controllerNamespace' =&gt; 'frontend\controllers',
  //'defaultRoute' =&gt; '/site/index',
  
  'components' =&gt; [
      'request' =&gt; [
          'baseUrl' =&gt; $baseUrl,
      ],
      'urlManager' =&gt; [
          'baseUrl' =&gt; $baseUrl,
          'class' =&gt; 'yii\web\UrlManager',
  
          'enablePrettyUrl' =&gt; true,
          'showScriptName' =&gt; false,
          'enableStrictParsing' =&gt; false,
          'rules' =&gt; [
              'home' =&gt; 'site/index' ,
              '/' =&gt; 'site/index', 
              'frontend/web' =&gt; 'site/index',
              'index' =&gt; 'site/index',
              'site' =&gt; 'site/index',
              'site/login' =&gt; 'site/login' ,
              'site/contact' =&gt; 'site/contact' ,
              'site/about' =&gt; 'site/about' ,
              'site/faq' =&gt; 'site/faq' ,
              'profile/logout' =&gt; 'profile/logout',
              'profile' =&gt; 'profile/index',
              'lesson/&lt;lesson:\w+&gt;/&lt;id:\d+&gt;' =&gt; 'courses/internal',
              'lesson' =&gt; 'courses/internal',
  
              'course/&lt;cat:\w+&gt;/&lt;id:\d+&gt;' =&gt; 'courses/index',
              'courses' =&gt; 'courses/index',
  
              'exam/&lt;cat:\w+&gt;/&lt;id:\d+&gt;' =&gt; 'exam/internal',
              'exam' =&gt; 'exam/index',
  
              'ticket' =&gt; 'ticket/index',
  
  
  
              'flashcard/&lt;cat:\w+&gt;/&lt;id:\d+&gt;' =&gt; 'blog/internal',
              'blog/&lt;cat:\w+&gt;/&lt;id:\d+&gt;' =&gt; 'blog/cat',
              'blog' =&gt; 'blog/index',
  
              '&lt;controller:\w+&gt;/&lt;action:\w+&gt;/&lt;id:\d+&gt;' =&gt; 
  '&lt;controller&gt;/&lt;action&gt;',
  
  
          ],
          ],
      'user' =&gt; [
          'identityClass' =&gt; 'common\models\User',
          'enableAutoLogin' =&gt; true,
      ],
      'log' =&gt; [
          'traceLevel' =&gt; YII_DEBUG ? 3 : 0,
          'targets' =&gt; [
              [
                  'class' =&gt; 'yii\log\FileTarget',
                  'levels' =&gt; ['error', 'warning'],
              ],
          ],
      ],
      'errorHandler' =&gt; [
          'errorAction' =&gt; 'site/error',
      ],
  
      'thumbnail' =&gt; [
          'class' =&gt; 'himiklab\thumbnail\EasyThumbnail',
          'cacheAlias' =&gt; 'assets/gallery_thumbnails',
      ],
      'assetManager' =&gt; [
          'class' =&gt; 'yii\web\AssetManager',
          'bundles' =&gt; [
                      'yii\web\JqueryAsset' =&gt; [
                          'js' =&gt; [
                              'jquery.min.js'
                          ]
                      ],
  
                      'yii\bootstrap\BootstrapAsset' =&gt; [
                          'css' =&gt; [
                             'css/bootstrap.min.css',
                          ]
                      ],
  
          ],
      ],
  ],
  'params' =&gt; $params,
  ];
  </code></pre>