diff --git a/README.md b/README.md new file mode 100644 index 0000000..e0c618a --- /dev/null +++ b/README.md @@ -0,0 +1,50 @@ +TaggedView +========== +Extension to help setup the standard HTML meta tags besides the ones defined by Opengraph and TwitterCard to contribute to website SEO + +Installation +------------ + +The preferred way to install this extension is through [composer](http://getcomposer.org/download/). + +Either run + +``` +php composer.phar require --prefer-dist daxslab/yii2-taggedview "*" +``` + +or add + +``` +"daxslab/yii2-taggedview": "*" +``` + +to the require section of your `composer.json` file. + +Configuration +------------- + +Configure the View component into the main configuration file of your application: + + 'components' => [ + ... + 'view' => [ + 'class' => 'daxslab\taggedview\View', + //configure some default values that will be shared by all the pages of the website + //if they are not overwritten by the page itself + 'image' => 'http://domain.com/images/default-image.jpg', + ], + ... + ] + +Usage +----- + +Once the extension is configured, simply use it in your views by: + + title = 'page title'; + $this->description = 'page description'; + $this->keywords = 'page keywords'; + $this->image = 'http://domain.com/images/page-image.jpg'; + ?> \ No newline at end of file diff --git a/View.php b/View.php new file mode 100755 index 0000000..503a21e --- /dev/null +++ b/View.php @@ -0,0 +1,174 @@ +site_name == null) { + $this->site_name = Yii::$app->name; + } + if ($this->url == null) { + $this->url = Yii::$app->request->absoluteUrl; + } + + $this->translateProperties(); + } + + protected function renderHeadHtml() + { + if ($this->locale == null) { + $this->locale = str_replace('-', '_', Yii::$app->language); + } + + if ($this->registerStandardTags) { + $this->registerStandardMetaTags(); + } + + if ($this->registerOpenGraphTags) { + $this->registerOpenGraphMetaTags(); + } + + if ($this->registerTwitterCardTags) { + $this->registerTwitterCardMetaTags(); + } + + array_multisort($this->metaTags); + return parent::renderHeadHtml(); + } + + protected function registerStandardMetaTags() + { + + foreach ($this->keywords as $keyword) { + $this->registerMetaTag(['name' => 'article:tag', 'content' => trim($keyword)]); + } + + $this->keywords = empty($this->keywords) ? null : join(', ', $this->keywords); + foreach (['author', 'description', 'robots', 'keywords', 'generator'] as $property) { + if ($this->$property) { + $this->registerMetaTag(['name' => $property, 'content' => $this->$property]); + } + } + + $this->registerLinkTag(['rel' => 'canonical', 'href' => $this->url]); + + } + + protected function registerOpenGraphMetaTags() + { + + $this->updated_time = $this->date; + + foreach (['title', 'url', 'site_name', 'type', 'description', 'locale', 'updated_time'] as $property) { + if ($this->$property) { + $this->registerMetaTag(['property' => "og:" . $property, 'content' => $this->$property]); + } + + } + + if ($this->image !== null) { + if (is_array($this->image)) { + foreach ($this->image as $key => $value) { + $this->registerMetaTag(['property' => 'og:image', 'content' => $value], 'og:image' . $key); + } + } else { + $this->registerMetaTag(['property' => 'og:image', 'content' => $this->image], 'og:image'); + } + + } + } + + + protected function registerTwitterCardMetaTags() + { + foreach ([ + 'card', + 'title', + 'url', + 'creator', + 'site', + 'type', + 'description', + 'label1', + 'data1', + 'label2', + 'data2' + ] as $property) { + if ($this->$property) { + $this->registerMetaTag(['name' => "twitter:" . $property, 'content' => $this->$property]); + } + } + + if ($this->image !== null) { + if (is_array($this->image)) { + $this->registerMetaTag(['name' => 'twitter:image', 'content' => $this->image[0]], 'twitter:image'); + } else { + $this->registerMetaTag(['name' => 'twitter:image', 'content' => $this->image], 'twitter:image'); + } + } + } + + protected function translateProperties() + { + foreach ($this->translate as $property) { + if ($this->$property != null) { + if (is_array($this->$property)) { + $the_array = $this->$property; + foreach ($the_array as $i => $word) { + $the_array[$i] = Yii::t('app', $word); + } + $this->$property = $the_array; + } else { + $this->$property = Yii::t('app', $this->$property); + } + } + } + } + +} \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..7c7c4de --- /dev/null +++ b/composer.json @@ -0,0 +1,21 @@ +{ + "name": "daxslab/yii2-taggedview", + "description": "Extension to help setup the standard HTML meta tags besides the ones defined by Opengraph and Twitter to contribute to website SEO", + "type": "yii2-extension", + "keywords": ["yii2","seo","html","metatags","facebook","twitter"], + "license": "MIT", + "authors": [ + { + "name": "Gabriel A. López López", + "email": "glpz@daxslab.com" + } + ], + "require": { + "yiisoft/yii2": "*" + }, + "autoload": { + "psr-4": { + "daxslab\\taggedview\\": "" + } + } +}