-
Notifications
You must be signed in to change notification settings - Fork 7
/
MetaTagsComponent.php
44 lines (35 loc) · 1.65 KB
/
MetaTagsComponent.php
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
43
44
<?php
/**
* Created by PhpStorm.
* User: artemshmanovsky
* Date: 13.03.15
* Time: 19:16
*/
namespace v0lume\yii2\metaTags;
use Yii;
class MetaTagsComponent extends \yii\base\Component {
public static $behaviorName = 'MetaTag';
public $generateCsrf = true;
public $generateOg = true;
public function register($model=null)
{
if($this->generateCsrf && Yii::$app->request->enableCsrfValidation)
{
Yii::$app->view->registerMetaTag(['name' => 'csrf-param', 'content' => Yii::$app->request->csrfParam], 'csrf-param');
Yii::$app->view->registerMetaTag(['name' => 'csrf-token', 'content' => Yii::$app->request->csrfToken], 'csrf-token');
}
if(isset($model) && $model->getBehavior(self::$behaviorName))
{
$meta_tag = $model->getBehavior(self::$behaviorName)->model;
Yii::$app->view->registerMetaTag(['name' => 'title', 'content' => $meta_tag->title], 'title');
Yii::$app->view->registerMetaTag(['name' => 'keywords', 'content' => $meta_tag->keywords], 'keywords');
Yii::$app->view->registerMetaTag(['name' => 'description', 'content' => $meta_tag->description], 'description');
if($this->generateOg)
{
Yii::$app->view->registerMetaTag(['property' => 'og:title', 'content' => $meta_tag->title], 'og:title');
Yii::$app->view->registerMetaTag(['property' => 'og:description', 'content' => $meta_tag->description], 'og:description');
Yii::$app->view->registerMetaTag(['property' => 'og:url', 'content' => \yii\helpers\Url::to('', true)], 'og:url');
}
}
}
}