Skip to content

Commit a6a58a9

Browse files
committed
Hello daxslab\taggedview!
0 parents  commit a6a58a9

3 files changed

Lines changed: 245 additions & 0 deletions

File tree

README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
TaggedView
2+
==========
3+
Extension to help setup the standard HTML meta tags besides the ones defined by Opengraph and TwitterCard to contribute to website SEO
4+
5+
Installation
6+
------------
7+
8+
The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
9+
10+
Either run
11+
12+
```
13+
php composer.phar require --prefer-dist daxslab/yii2-taggedview "*"
14+
```
15+
16+
or add
17+
18+
```
19+
"daxslab/yii2-taggedview": "*"
20+
```
21+
22+
to the require section of your `composer.json` file.
23+
24+
Configuration
25+
-------------
26+
27+
Configure the View component into the main configuration file of your application:
28+
29+
'components' => [
30+
...
31+
'view' => [
32+
'class' => 'daxslab\taggedview\View',
33+
//configure some default values that will be shared by all the pages of the website
34+
//if they are not overwritten by the page itself
35+
'image' => 'http://domain.com/images/default-image.jpg',
36+
],
37+
...
38+
]
39+
40+
Usage
41+
-----
42+
43+
Once the extension is configured, simply use it in your views by:
44+
45+
<?php
46+
$this->title = 'page title';
47+
$this->description = 'page description';
48+
$this->keywords = 'page keywords';
49+
$this->image = 'http://domain.com/images/page-image.jpg';
50+
?>

View.php

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
<?php
2+
3+
/*
4+
* To change this license header, choose License Headers in Project Properties.
5+
* To change this template file, choose Tools | Templates
6+
* and open the template in the editor.
7+
*/
8+
9+
namespace daxslab\taggedview;
10+
11+
use Yii;
12+
use yii\web\View as BaseView;
13+
14+
/**
15+
* Description of SeoTags
16+
*
17+
* @author glpz
18+
*/
19+
class View extends BaseView
20+
{
21+
22+
// toogle tags to register
23+
public $registerStandardTags = true;
24+
public $registerOpenGraphTags = true;
25+
public $registerTwitterCardTags = true;
26+
// toogle translations
27+
public $translate = ['title', 'site_name', 'description', 'author', 'keywords'];
28+
//meta properties
29+
public $author;
30+
public $site_name;
31+
public $url;
32+
public $description;
33+
public $type;
34+
public $locale;
35+
public $image;
36+
public $robots;
37+
public $keywords = [];
38+
public $creator;
39+
public $generator;
40+
public $date;
41+
public $data_type;
42+
public $card;
43+
public $site;
44+
public $label1;
45+
public $data1;
46+
public $label2;
47+
public $data2;
48+
49+
private $updated_time;
50+
51+
public function init()
52+
{
53+
if ($this->site_name == null) {
54+
$this->site_name = Yii::$app->name;
55+
}
56+
if ($this->url == null) {
57+
$this->url = Yii::$app->request->absoluteUrl;
58+
}
59+
60+
$this->translateProperties();
61+
}
62+
63+
protected function renderHeadHtml()
64+
{
65+
if ($this->locale == null) {
66+
$this->locale = str_replace('-', '_', Yii::$app->language);
67+
}
68+
69+
if ($this->registerStandardTags) {
70+
$this->registerStandardMetaTags();
71+
}
72+
73+
if ($this->registerOpenGraphTags) {
74+
$this->registerOpenGraphMetaTags();
75+
}
76+
77+
if ($this->registerTwitterCardTags) {
78+
$this->registerTwitterCardMetaTags();
79+
}
80+
81+
array_multisort($this->metaTags);
82+
return parent::renderHeadHtml();
83+
}
84+
85+
protected function registerStandardMetaTags()
86+
{
87+
88+
foreach ($this->keywords as $keyword) {
89+
$this->registerMetaTag(['name' => 'article:tag', 'content' => trim($keyword)]);
90+
}
91+
92+
$this->keywords = empty($this->keywords) ? null : join(', ', $this->keywords);
93+
foreach (['author', 'description', 'robots', 'keywords', 'generator'] as $property) {
94+
if ($this->$property) {
95+
$this->registerMetaTag(['name' => $property, 'content' => $this->$property]);
96+
}
97+
}
98+
99+
$this->registerLinkTag(['rel' => 'canonical', 'href' => $this->url]);
100+
101+
}
102+
103+
protected function registerOpenGraphMetaTags()
104+
{
105+
106+
$this->updated_time = $this->date;
107+
108+
foreach (['title', 'url', 'site_name', 'type', 'description', 'locale', 'updated_time'] as $property) {
109+
if ($this->$property) {
110+
$this->registerMetaTag(['property' => "og:" . $property, 'content' => $this->$property]);
111+
}
112+
113+
}
114+
115+
if ($this->image !== null) {
116+
if (is_array($this->image)) {
117+
foreach ($this->image as $key => $value) {
118+
$this->registerMetaTag(['property' => 'og:image', 'content' => $value], 'og:image' . $key);
119+
}
120+
} else {
121+
$this->registerMetaTag(['property' => 'og:image', 'content' => $this->image], 'og:image');
122+
}
123+
124+
}
125+
}
126+
127+
128+
protected function registerTwitterCardMetaTags()
129+
{
130+
foreach ([
131+
'card',
132+
'title',
133+
'url',
134+
'creator',
135+
'site',
136+
'type',
137+
'description',
138+
'label1',
139+
'data1',
140+
'label2',
141+
'data2'
142+
] as $property) {
143+
if ($this->$property) {
144+
$this->registerMetaTag(['name' => "twitter:" . $property, 'content' => $this->$property]);
145+
}
146+
}
147+
148+
if ($this->image !== null) {
149+
if (is_array($this->image)) {
150+
$this->registerMetaTag(['name' => 'twitter:image', 'content' => $this->image[0]], 'twitter:image');
151+
} else {
152+
$this->registerMetaTag(['name' => 'twitter:image', 'content' => $this->image], 'twitter:image');
153+
}
154+
}
155+
}
156+
157+
protected function translateProperties()
158+
{
159+
foreach ($this->translate as $property) {
160+
if ($this->$property != null) {
161+
if (is_array($this->$property)) {
162+
$the_array = $this->$property;
163+
foreach ($the_array as $i => $word) {
164+
$the_array[$i] = Yii::t('app', $word);
165+
}
166+
$this->$property = $the_array;
167+
} else {
168+
$this->$property = Yii::t('app', $this->$property);
169+
}
170+
}
171+
}
172+
}
173+
174+
}

composer.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "daxslab/yii2-taggedview",
3+
"description": "Extension to help setup the standard HTML meta tags besides the ones defined by Opengraph and Twitter to contribute to website SEO",
4+
"type": "yii2-extension",
5+
"keywords": ["yii2","seo","html","metatags","facebook","twitter"],
6+
"license": "MIT",
7+
"authors": [
8+
{
9+
"name": "Gabriel A. López López",
10+
"email": "glpz@daxslab.com"
11+
}
12+
],
13+
"require": {
14+
"yiisoft/yii2": "*"
15+
},
16+
"autoload": {
17+
"psr-4": {
18+
"daxslab\\taggedview\\": ""
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)