Мета-теги — это html-теги, с помощью которых указываются дополнительные данные о странице сайта. Влияют на то, как отображается и какую позицию занимает страница в поисковой выдаче. При прочих равных условиях поисковики отдают предпочтение сайту с грамотно сформированными мета-тегами.
Установка мета-тегов через контроллер:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?php namespace app\controllers; use Yii; use yii\web\Controller; class SiteController extends Controller { public function actionIndex() { return $this ->render( 'index' ); } public function actionHello() { $this ->view->title = 'Заголовок страницы' ; $this ->view->registerMetaTag( [ 'name' => 'keywords' , 'content' => 'Ключевые слова страницы' ] ); $this ->view->registerMetaTag( [ 'name' => 'description' , 'content' => 'Краткое описание страницы' ] ); return $this ->render( 'hello' ); } } |
В layout-шаблон добавить вызов метода head():
1 2 3 4 5 6 | <head> <meta charset= "<?= Yii::$app->charset ?>" > <meta name= "viewport" content= "width=device-width, initial-scale=1" > <title><?= Html::encode( $this ->title) ?></title> <?php $this ->head() ?> </head> |
В результате получим
1 2 3 4 5 6 7 8 9 | <head> <meta charset= "UTF-8" > <meta name= "viewport" content= "width=device-width, initial-scale=1" > <title>Заголовок страницы</title> <meta name= "keywords" content= "Ключевые слова страницы" > <meta name= "description" content= "Краткое описание страницы" > <link href= "/web/assets/ca646842/css/bootstrap.css" rel= "stylesheet" > <link href= "/web/css/site.css" rel= "stylesheet" > </head> |
Мета-теги можно зарегистрировать и в view-шаблоне:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?php /* @var $this yii\web\View */ use yii\helpers\Html; $this ->title = 'Заголовок страницы 2' ; $this ->registerMetaTag( [ 'name' => 'keywords' , 'content' => 'Ключевые слова страницы 2' ] ); $this ->registerMetaTag( [ 'name' => 'description' , 'content' => 'Краткое описание страницы 2' ] ); ?> <div class = "site-hello" > <h1><?= Html::encode( $this ->title) ?></h1> <p>Вы можете изменять эту страницу, редактируя файл шаблона:</p> <code><?= __FILE__ ?></code> </div> |
Результат будет аналогичный:
1 2 3 4 5 6 7 8 9 | <head> <meta charset= "UTF-8" > <meta name= "viewport" content= "width=device-width, initial-scale=1" > <title>Заголовок страницы 2</title> <meta name= "keywords" content= "Ключевые слова страницы 2" > <meta name= "description" content= "Краткое описание страницы 2" > <link href= "/web/assets/ca646842/css/bootstrap.css" rel= "stylesheet" > <link href= "/web/css/site.css" rel= "stylesheet" > </head> |
Установка $this->title в шаблоне переопределит значение, установленное в контроллере. Здесь стоит обратить внимание на следующее: если зарегистрировать один и тот же тег дважды, то этот мета-тег два раза и будет добавлен на страницу. Такая ситуация может возникнуть в том случае, когда мета-тег был зарегистрирован в контроллере и в шаблоне.
1 2 3 4 5 6 7 8 9 10 11 | <head> <meta charset= "UTF-8" > <meta name= "viewport" content= "width=device-width, initial-scale=1" > <title>Заголовок страницы 2</title> <meta name= "keywords" content= "Ключевые слова страницы" > <meta name= "description" content= "Краткое описание страницы" > <meta name= "keywords" content= "Ключевые слова страницы 2" > <meta name= "description" content= "Краткое описание страницы 2" > <link href= "/web/assets/ca646842/css/bootstrap.css" rel= "stylesheet" > <link href= "/web/css/site.css" rel= "stylesheet" > </head> |
Чтобы этого избежать, необходимо использовать дополнительный аргумент:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | <?php namespace app\controllers; use Yii; use yii\web\Controller; class SiteController extends Controller { public function actionIndex() { return $this ->render( 'index' ); } public function actionHello() { $this ->view->title = 'Заголовок страницы' ; $this ->view->registerMetaTag( [ 'name' => 'keywords' , 'content' => 'Ключевые слова страницы' ], 'keywords' ); $this ->view->registerMetaTag( [ 'name' => 'description' , 'content' => 'Краткое описание страницы' ], 'description' ); return $this ->render( 'hello' ); } } <?php /* @var $this yii\web\View */ use yii\helpers\Html; $this ->title = 'Заголовок страницы 2' ; $this ->registerMetaTag( [ 'name' => 'keywords' , 'content' => 'Ключевые слова страницы 2' ], 'keywords' ); $this ->registerMetaTag( [ 'name' => 'description' , 'content' => 'Краткое описание страницы 2' ], 'description' ); ?> <div class = "site-hello" > <h1><?= Html::encode( $this ->title) ?></h1> <p>Вы можете изменять эту страницу, редактируя файл шаблона:</p> <code><?= __FILE__ ?></code> </div> |
В этом случае произойдет перезапись предыдущих данных.