├── migrations ├── readme.txt └── m141023_143432_table_meta.php ├── AutoloadExample.php ├── Module.php ├── views └── manage │ ├── create.php │ ├── update.php │ ├── index.php │ ├── view.php │ ├── _search.php │ └── _form.php ├── composer.json ├── LICENCE.md ├── models ├── Meta.php ├── base │ └── MetaBase.php └── MetaSearch.php ├── README.md ├── controllers └── ManageController.php └── Component.php /migrations/readme.txt: -------------------------------------------------------------------------------- 1 | To run the migrations for this extension go to your app folder and run the following command 2 | 3 | ./yii migrate --migrationPath="@vendor/jpunanua/yii2-seo/migrations" -------------------------------------------------------------------------------- /AutoloadExample.php: -------------------------------------------------------------------------------- 1 | title = Yii::t('seotools', 'Create Meta Base'); 10 | $this->params['breadcrumbs'][] = ['label' => Yii::t('seotools', 'Meta Bases'), 'url' => ['index']]; 11 | $this->params['breadcrumbs'][] = $this->title; 12 | ?> 13 |
14 | 15 |

title) ?>

16 | 17 | render('_form', [ 18 | 'model' => $model, 19 | ]) ?> 20 | 21 |
22 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jpunanua/yii2-seotools", 3 | "description": "Component and module to manage unique seo title, description, keywords and unique text associated with a page", 4 | "type": "yii2-extension", 5 | "keywords": ["yii2","extension","seo","management","optimize","metas","description","title"], 6 | "license": "MIT", 7 | "authors": [ 8 | { 9 | "name": "Javier Pérez Unanua", 10 | "email": "javierperezu@hotmail.com" 11 | } 12 | ], 13 | "require": { 14 | "yiisoft/yii2": "*", 15 | "mihaildev/yii2-ckeditor": "*" 16 | }, 17 | "autoload": { 18 | "psr-4": { 19 | "jpunanua\\seotools\\": "" 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /views/manage/update.php: -------------------------------------------------------------------------------- 1 | title = Yii::t('seotools', 'Update {modelClass}: ', [ 9 | 'modelClass' => 'Meta Base', 10 | ]) . ' ' . $model->title; 11 | $this->params['breadcrumbs'][] = ['label' => Yii::t('seotools', 'Meta Bases'), 'url' => ['index']]; 12 | $this->params['breadcrumbs'][] = ['label' => $model->title, 'url' => ['view', 'id' => $model->id_meta]]; 13 | $this->params['breadcrumbs'][] = Yii::t('seotools', 'Update'); 14 | ?> 15 |
16 | 17 |

title) ?>

18 | 19 | render('_form', [ 20 | 'model' => $model, 21 | ]) ?> 22 | 23 |
24 | -------------------------------------------------------------------------------- /LICENCE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | ==================== 3 | 4 | Copyright (c) 2014 Lajos Molnar 5 | 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. -------------------------------------------------------------------------------- /views/manage/index.php: -------------------------------------------------------------------------------- 1 | title = Yii::t('seotools', 'Meta Bases'); 11 | $this->params['breadcrumbs'][] = $this->title; 12 | ?> 13 |
14 | 15 |

title) ?>

16 | render('_search', ['model' => $searchModel]); ?> 17 | 18 |

19 | 'btn btn-success']) ?> 20 |

21 | 22 | $dataProvider, 24 | 'filterModel' => $searchModel, 25 | 'columns' => [ 26 | ['class' => 'yii\grid\SerialColumn'], 27 | 28 | 'id_meta', 29 | 'hash', 30 | 'route', 31 | 'robots_index', 32 | 'robots_follow', 33 | // 'author', 34 | // 'title', 35 | // 'keywords:ntext', 36 | // 'description:ntext', 37 | // 'info:ntext', 38 | // 'sitemap', 39 | // 'sitemap_change_freq', 40 | // 'sitemap_priority', 41 | // 'created_at', 42 | // 'updated_at', 43 | 44 | ['class' => 'yii\grid\ActionColumn'], 45 | ], 46 | ]); ?> 47 | 48 |
49 | -------------------------------------------------------------------------------- /models/Meta.php: -------------------------------------------------------------------------------- 1 | TimestampBehavior::className(), 25 | 'createdAtAttribute' => 'created_at', 26 | 'updatedAtAttribute' => 'updated_at', 27 | 'value' => new Expression('NOW()'), 28 | ], 29 | ]; 30 | } 31 | 32 | /** 33 | * @inheritdoc 34 | */ 35 | public function init() 36 | { 37 | parent::init(); 38 | $this->created_at = new Expression('NOW()'); 39 | $this->updated_at = new Expression('NOW()'); 40 | } 41 | 42 | /** 43 | * @inheritdoc 44 | */ 45 | public function beforeValidate() 46 | { 47 | $this->hash = md5($this->route); 48 | return parent::beforeValidate(); 49 | } 50 | 51 | /** 52 | * @inheritdoc 53 | */ 54 | public function beforeSave($insert) 55 | { 56 | $this->hash = md5($this->route); 57 | return parent::beforeSave($insert); 58 | } 59 | 60 | public function setRoute($route) { 61 | $this->route = $route; 62 | $this->hash = md5($this->route); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /views/manage/view.php: -------------------------------------------------------------------------------- 1 | title = $model->title; 10 | $this->params['breadcrumbs'][] = ['label' => Yii::t('seotools', 'Meta Bases'), 'url' => ['index']]; 11 | $this->params['breadcrumbs'][] = $this->title; 12 | ?> 13 |
14 | 15 |

title) ?>

16 | 17 |

18 | $model->id_meta], ['class' => 'btn btn-primary']) ?> 19 | $model->id_meta], [ 20 | 'class' => 'btn btn-danger', 21 | 'data' => [ 22 | 'confirm' => Yii::t('seotools', 'Are you sure you want to delete this item?'), 23 | 'method' => 'post', 24 | ], 25 | ]) ?> 26 |

27 | 28 | $model, 30 | 'attributes' => [ 31 | 'id_meta', 32 | 'hash', 33 | 'route', 34 | 'robots_index', 35 | 'robots_follow', 36 | 'author', 37 | 'title', 38 | 'keywords:ntext', 39 | 'description:ntext', 40 | 'info:ntext', 41 | 'sitemap', 42 | 'sitemap_change_freq', 43 | 'sitemap_priority', 44 | 'created_at', 45 | 'updated_at', 46 | ], 47 | ]) ?> 48 | 49 |
50 | -------------------------------------------------------------------------------- /migrations/m141023_143432_table_meta.php: -------------------------------------------------------------------------------- 1 | db->driverName === 'mysql') { 12 | $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB'; 13 | } 14 | 15 | /* MYSQL */ 16 | $this->createTable('{{%meta}}', [ 17 | 'id_meta' => 'INT(11) UNSIGNED NOT NULL AUTO_INCREMENT', 18 | 'hash' => 'VARCHAR(255) NOT NULL', 19 | 'route' => 'VARCHAR(255) NOT NULL', 20 | 'robots_index' => 'ENUM(\'INDEX\',\'NOINDEX\') NULL', 21 | 'robots_follow' => 'ENUM(\'FOLLOW\',\'NOFOLLOW\') NULL', 22 | 'author' => 'VARCHAR(255) NULL', 23 | 'title' => 'VARCHAR(255) NULL', 24 | 'keywords' => 'TEXT NULL', 25 | 'description' => 'TEXT NULL', 26 | 'info' => 'TEXT NULL', 27 | 'sitemap' => 'TINYINT(1) UNSIGNED NOT NULL DEFAULT \'1\'', 28 | 'sitemap_change_freq' => 'VARCHAR(20) NULL', 29 | 'sitemap_priority' => 'VARCHAR(4) NULL', 30 | 'created_at' => Schema::TYPE_DATETIME . ' NOT NULL', 31 | 'updated_at' => Schema::TYPE_DATETIME . ' NOT NULL', 32 | 0 => 'PRIMARY KEY (`id_meta`)' 33 | ], $tableOptions); 34 | 35 | $this->createIndex('hash', '{{%meta}}', 'hash', true); 36 | } 37 | 38 | public function down() 39 | { 40 | $this->dropTable('{{%meta}}'); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /views/manage/_search.php: -------------------------------------------------------------------------------- 1 | 10 | 11 | 56 | -------------------------------------------------------------------------------- /views/manage/_form.php: -------------------------------------------------------------------------------- 1 | 11 | 12 |
13 | 14 | 15 | 16 | field($model, 'route')->textInput(['maxlength' => 255]) ?> 17 | 18 | field($model, 'robots_index')->dropDownList([ 'INDEX' => 'INDEX', 'NOINDEX' => 'NOINDEX', ], ['prompt' => '']) ?> 19 | 20 | field($model, 'robots_follow')->dropDownList([ 'FOLLOW' => 'FOLLOW', 'NOFOLLOW' => 'NOFOLLOW', ], ['prompt' => '']) ?> 21 | 22 | field($model, 'author')->textInput(['maxlength' => 255]) ?> 23 | 24 | field($model, 'title')->textInput(['maxlength' => 255]) ?> 25 | 26 | field($model, 'keywords')->textarea(['rows' => 6]) ?> 27 | 28 | field($model, 'description')->textarea(['rows' => 6]) ?> 29 | 30 | field($model, 'info')->widget(CKEditor::className(),[ 32 | 'editorOptions' => [ 33 | 'preset' => 'full', 34 | 'inline' => false 35 | ], 36 | ]); 37 | ?> 38 | 39 | field($model, 'sitemap')->textInput() ?> 40 | 41 | field($model, 'sitemap_change_freq')->textInput(['maxlength' => 20]) ?> 42 | 43 | field($model, 'sitemap_priority')->textInput(['maxlength' => 4]) ?> 44 | 45 |
46 | isNewRecord ? Yii::t('seotools', 'Create') : Yii::t('seotools', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?> 47 |
48 | 49 | 50 | 51 |
52 | -------------------------------------------------------------------------------- /models/base/MetaBase.php: -------------------------------------------------------------------------------- 1 | 255], 48 | [['sitemap_change_freq'], 'string', 'max' => 20], 49 | [['sitemap_priority'], 'string', 'max' => 4], 50 | [['hash'], 'unique'] 51 | ]; 52 | } 53 | 54 | /** 55 | * @inheritdoc 56 | */ 57 | public function attributeLabels() 58 | { 59 | return [ 60 | 'id_meta' => 'Id Meta', 61 | 'hash' => 'Hash', 62 | 'route' => 'Route', 63 | 'robots_index' => 'Robots Index', 64 | 'robots_follow' => 'Robots Follow', 65 | 'author' => 'Author', 66 | 'title' => 'Title', 67 | 'keywords' => 'Keywords', 68 | 'description' => 'Description', 69 | 'sitemap' => 'Sitemap', 70 | 'sitemap_change_freq' => 'Sitemap Change Freq', 71 | 'sitemap_priority' => 'Sitemap Priority', 72 | 'created_at' => 'Created At', 73 | 'updated_at' => 'Updated At', 74 | ]; 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /models/MetaSearch.php: -------------------------------------------------------------------------------- 1 | $query, 48 | ]); 49 | 50 | $this->load($params); 51 | 52 | if (!$this->validate()) { 53 | // uncomment the following line if you do not want to any records when validation fails 54 | // $query->where('0=1'); 55 | return $dataProvider; 56 | } 57 | 58 | $query->andFilterWhere([ 59 | 'id_meta' => $this->id_meta, 60 | 'sitemap' => $this->sitemap, 61 | 'created_at' => $this->created_at, 62 | 'updated_at' => $this->updated_at, 63 | ]); 64 | 65 | $query->andFilterWhere(['like', 'hash', $this->hash]) 66 | ->andFilterWhere(['like', 'route', $this->route]) 67 | ->andFilterWhere(['like', 'robots_index', $this->robots_index]) 68 | ->andFilterWhere(['like', 'robots_follow', $this->robots_follow]) 69 | ->andFilterWhere(['like', 'author', $this->author]) 70 | ->andFilterWhere(['like', 'title', $this->title]) 71 | ->andFilterWhere(['like', 'keywords', $this->keywords]) 72 | ->andFilterWhere(['like', 'description', $this->description]) 73 | ->andFilterWhere(['like', 'info', $this->info]) 74 | ->andFilterWhere(['like', 'sitemap_change_freq', $this->sitemap_change_freq]) 75 | ->andFilterWhere(['like', 'sitemap_priority', $this->sitemap_priority]); 76 | 77 | return $dataProvider; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Tool to manage particular SEO Metas and text for especial pages 2 | =============================================================== 3 | If you need set unique seo title, description associated with a page this is your extension, you can also add a html text using 4 | a wysiwis tool to add bold and links and improve your SEO in page with a unique content. 5 | 6 | Set this fields using a module to manage all this functionality. 7 | 8 | Use internally a md5 hash to made a unique id with (Host + Path) to identify pages and yii cache system and tag dependency to improve speed 9 | 10 | Installation 11 | ------------ 12 | 13 | The preferred way to install this extension is through [composer](http://getcomposer.org/download/). 14 | 15 | Either run 16 | 17 | ``` 18 | php composer.phar require --prefer-dist jpunanua/yii2-seotools "*" 19 | ``` 20 | 21 | or add 22 | 23 | ``` 24 | "jpunanua/yii2-seotools": "*" 25 | ``` 26 | 27 | to the require section of your `composer.json` file. 28 | 29 | ###Migration 30 | 31 | 32 | Run the following command in Terminal for database migration: 33 | 34 | Linux/Unix: 35 | ``` 36 | yii migrate/up --migrationPath=@vendor/jpunanua/yii2-seotools/migrations 37 | ``` 38 | 39 | Windows: 40 | ``` 41 | yii.bat migrate/up --migrationPath=@vendor/jpunanua/yii2-seotools/migrations 42 | ``` 43 | 44 | ###Config 45 | 46 | A simple exmple of turning on seotool component. 47 | 48 | ```php 49 | 'components' => [ 50 | 'seotools' => [ 51 | 'class' => 'jpunanua\seotools\Component', 52 | ], 53 | ], 54 | ``` 55 | 56 | 57 | Turning on the seotools Module: 58 | 59 | 60 | Simple example: 61 | 62 | ```php 63 | 'modules' => [ 64 | 'seotools' => [ 65 | 'class' => 'jpunanua\seotools\Module', 66 | 'roles' => ['@'], // For setting access levels to the seotools interface. 67 | ] 68 | ], 69 | ``` 70 | 71 | Usage 72 | ----- 73 | 74 | Once the extension is installed, simply use it in your code by : 75 | 76 | ```php 77 | // @param bool $setCanonical true, try to create a canonical url and og url, action needs to have params 78 | // @param bool $checkDb try to get from DB params, true: try to get info from DB if it doesn't find save a new field 79 | // associated to current host + '/' + path, false: it just set the params give in the call. The db params has priority 80 | // over the call function params. It does a merge 81 | $setCanonical = false; 82 | $checkDb = true; 83 | Yii::$app->seotools->setMeta(['title' => \Yii::t('title','A good title for this page')], $setCanonical, $checkDb); 84 | ``` 85 | 86 | You can invalidate the cache save records calling 87 | 88 | ```php 89 | \yii\caching\TagDependency::invalidate(Yii::$app->cache, jpunanua\seotools\Component::CACHE_TAG); 90 | ``` 91 | 92 | ###URLs 93 | 94 | URLs for the seotools manage module: 95 | 96 | ```php 97 | /seotools/manage 98 | /seotools/manage/create 99 | ``` 100 | 101 | -------------------------------------------------------------------------------- /controllers/ManageController.php: -------------------------------------------------------------------------------- 1 | [ 31 | 'class' => VerbFilter::className(), 32 | 'actions' => [ 33 | 'delete' => ['post'], 34 | ], 35 | ], 36 | 'access' => [ 37 | 'class' => AccessControl::className(), 38 | 'rules' => [ 39 | [ 40 | 'allow' => true, 41 | 'roles' => $this->module->roles, 42 | ], 43 | ], 44 | ], 45 | ]; 46 | } 47 | 48 | /** 49 | * Lists all MetaBase models. 50 | * @return mixed 51 | */ 52 | public function actionIndex() 53 | { 54 | $searchModel = new MetaSearch(); 55 | $dataProvider = $searchModel->search(Yii::$app->request->queryParams); 56 | 57 | return $this->render('index', [ 58 | 'searchModel' => $searchModel, 59 | 'dataProvider' => $dataProvider, 60 | ]); 61 | } 62 | 63 | /** 64 | * Displays a single MetaBase model. 65 | * @param integer $id 66 | * @return mixed 67 | */ 68 | public function actionView($id) 69 | { 70 | return $this->render('view', [ 71 | 'model' => $this->findModel($id), 72 | ]); 73 | } 74 | 75 | /** 76 | * Creates a new MetaBase model. 77 | * If creation is successful, the browser will be redirected to the 'view' page. 78 | * @return mixed 79 | */ 80 | public function actionCreate() 81 | { 82 | $model = new Meta(); 83 | 84 | if ($model->load(Yii::$app->request->post()) && $model->save()) { 85 | return $this->redirect(['view', 'id' => $model->id_meta]); 86 | } else { 87 | return $this->render('create', [ 88 | 'model' => $model, 89 | ]); 90 | } 91 | } 92 | 93 | /** 94 | * Updates an existing MetaBase model. 95 | * If update is successful, the browser will be redirected to the 'view' page. 96 | * @param integer $id 97 | * @return mixed 98 | */ 99 | public function actionUpdate($id) 100 | { 101 | $model = $this->findModel($id); 102 | 103 | if ($model->load(Yii::$app->request->post()) && $model->save()) { 104 | return $this->redirect(['view', 'id' => $model->id_meta]); 105 | } else { 106 | return $this->render('update', [ 107 | 'model' => $model, 108 | ]); 109 | } 110 | } 111 | 112 | /** 113 | * Deletes an existing MetaBase model. 114 | * If deletion is successful, the browser will be redirected to the 'index' page. 115 | * @param integer $id 116 | * @return mixed 117 | */ 118 | public function actionDelete($id) 119 | { 120 | $this->findModel($id)->delete(); 121 | 122 | return $this->redirect(['index']); 123 | } 124 | 125 | /** 126 | * Finds the MetaBase model based on its primary key value. 127 | * If the model is not found, a 404 HTTP exception will be thrown. 128 | * @param integer $id 129 | * @return MetaBase the loaded model 130 | * @throws NotFoundHttpException if the model cannot be found 131 | */ 132 | protected function findModel($id) 133 | { 134 | if (($model = Meta::findOne($id)) !== null) { 135 | return $model; 136 | } else { 137 | throw new NotFoundHttpException('The requested page does not exist.'); 138 | } 139 | } 140 | } 141 | -------------------------------------------------------------------------------- /Component.php: -------------------------------------------------------------------------------- 1 | 'website', 21 | ]; 22 | 23 | /** 24 | * Component ID representing the database 25 | * @var string 26 | */ 27 | public $db = 'db'; 28 | 29 | /** 30 | * Component ID representing the cache 31 | * @var string 32 | */ 33 | public $cache = 'cache'; 34 | 35 | /** 36 | * The Component ID for this module (used to mark cache segments) 37 | * @var string 38 | */ 39 | public $componentId = 'seotools'; 40 | 41 | /** 42 | * After how long (seconds) will the routes caching expire. 43 | * @var int 44 | */ 45 | public $cacheDuration = 3600; 46 | 47 | /** 48 | * host + path: used to identify pages 49 | * @var null 50 | */ 51 | public $route = null; 52 | 53 | /** 54 | * chache tag dependency 55 | * 56 | * @var string 57 | */ 58 | const CACHE_TAG = 'seotools'; 59 | 60 | 61 | private $_info = ''; 62 | 63 | 64 | /** 65 | * Devuelve la url absoluta con el path 66 | * @return string 67 | */ 68 | public function getRoute() { 69 | if (is_null($this->route)) { 70 | $this->route = Yii::$app->request->getHostInfo() . '/' . Yii::$app->request->getPathInfo(); 71 | } 72 | return $this->route; 73 | } 74 | 75 | 76 | /** 77 | * @param string $route 78 | * @return array 79 | */ 80 | public function getMeta($route) 81 | { 82 | $cache = Yii::$app->{$this->cache}; 83 | $cacheId = $this->componentId . '|routes|' . $route; 84 | $aMeta = $cache->get($cacheId); 85 | 86 | if ($aMeta) { 87 | return $aMeta; 88 | } 89 | 90 | $oMeta = new Meta(); 91 | $oMeta->setRoute($route); 92 | 93 | $aMeta = []; 94 | $model = Meta::findOne([ 95 | 'hash' => $oMeta->hash 96 | ]); 97 | 98 | if (!empty($model)) { 99 | $info = $model->toArray(); 100 | 101 | foreach ($info as $idData => $data) { 102 | if (!empty($data)) { 103 | $aMeta[$idData] = $data; 104 | } 105 | } 106 | } else { 107 | // Si no existe la entrada con esa ruta la creamos 108 | $oMeta->save(); 109 | } 110 | 111 | $oTagDependency = new \yii\caching\TagDependency(['tags' => self::CACHE_TAG ]); 112 | 113 | $cache->set($cacheId, $aMeta, $this->cacheDuration, $oTagDependency); 114 | 115 | return $aMeta; 116 | } 117 | 118 | /** 119 | * Register the robots meta 120 | * $index must be index or noindex or empty/null 121 | * $follow must be follow or nofollow or empty/null 122 | * @param string $index 123 | * @param string $follow 124 | */ 125 | public function setRobots($index = '', $follow = '') 126 | { 127 | $v = []; 128 | 129 | if (!empty($index)) { 130 | $v[] = $index; 131 | } 132 | 133 | if (!empty($follow)) { 134 | $v[] = $follow; 135 | } 136 | 137 | if (!empty($v)) { 138 | Yii::$app->view->registerMetaTag(['name' => 'robots', 'content' => strtolower(implode(',', $v))], 'robots'); 139 | } 140 | return $this; 141 | } 142 | 143 | /** 144 | * Register the author meta 145 | * @param string $author 146 | */ 147 | public function setAuthor($author) 148 | { 149 | if (!empty($author)) { 150 | Yii::$app->view->registerMetaTag(['name' => 'author', 'content' => $author], 'author'); 151 | } 152 | return $this; 153 | } 154 | 155 | /** 156 | * Register Open Graph Type meta 157 | * @param string $type 158 | */ 159 | public function setOpenGraphType($type) 160 | { 161 | if (!empty($type)) { 162 | Yii::$app->view->registerMetaTag(['name' => 'og:type', 'content' => $type], 'og:type'); 163 | } 164 | return $this; 165 | } 166 | 167 | /** 168 | * Register title meta and open graph title meta 169 | * @param string $title 170 | */ 171 | public function setTitle($title) 172 | { 173 | if (!empty($title)) { 174 | Yii::$app->view->registerMetaTag(['name' => 'title', 'content' => $title], 'title'); 175 | Yii::$app->view->registerMetaTag(['name' => 'og:title', 'content' => $title], 'og:title'); 176 | Yii::$app->view->title = $title; 177 | } 178 | return $this; 179 | } 180 | 181 | /** 182 | * Register description meta and open graph description meta 183 | * @param string $description 184 | */ 185 | public function setDescription($description) 186 | { 187 | if (!empty($description)) { 188 | Yii::$app->view->registerMetaTag(['name' => 'description', 'content' => $description], 'description'); 189 | Yii::$app->view->registerMetaTag(['name' => 'og:description', 'content' => $description], 'og:description'); 190 | } 191 | return $this; 192 | } 193 | 194 | /** 195 | * Register keywords meta 196 | * @param string $keywords 197 | */ 198 | public function setKeywords($keywords) 199 | { 200 | if (!empty($keywords)) { 201 | Yii::$app->view->registerMetaTag(['name' => 'keywords', 'content' => $keywords], 'keywords'); 202 | } 203 | return $this; 204 | } 205 | 206 | /** 207 | * Register Canonical url 208 | * @param string $url 209 | */ 210 | public function setCanonical($url) 211 | { 212 | Yii::$app->view->registerLinkTag(['href' => $url, 'rel' => 'canonical'], 'canonical'); 213 | return $this; 214 | } 215 | 216 | /** 217 | * Register Open Graph Page Url 218 | * @param string $url 219 | */ 220 | public function setOpenGraphUrl($url) 221 | { 222 | Yii::$app->view->registerMetaTag(['name' => 'og:url', 'content' => $url], 'og:url'); 223 | return $this; 224 | } 225 | 226 | /** 227 | * Register text associated to a Url 228 | * @param string $info 229 | */ 230 | public function setInfotext($info) 231 | { 232 | $this->_info = $info; 233 | return $this; 234 | } 235 | 236 | 237 | public function getInfotext() { 238 | return $this->_info; 239 | } 240 | 241 | /** 242 | * @param array $metadata 243 | * @param bool $setCanonical true, try to create a canonical url and og url, action needs to have params 244 | * @param bool $checkDb try to get from DB params 245 | */ 246 | public function setMeta($metadata = [], $setCanonical = false, $checkDb = false) 247 | { 248 | // Set to empty not given values 249 | $metadataReset = ['robots_index' => '', 'robots_follow' => '', 'author' => '', 250 | 'title' => '', 'description' => '', 'info' =>'','keywords' => '', 'keywords' => '', 'params_url' => '']; 251 | 252 | $metadata = array_merge($metadataReset, $metadata); 253 | 254 | if ($checkDb) { 255 | // Merge passed parameter meta with route meta 256 | $metadata = array_merge($metadata, $this->getMeta($this->getRoute())); 257 | } 258 | 259 | // Override meta with the defaults via merge 260 | $metadata = array_merge($metadata, $this->defaults); 261 | 262 | $this->setRobots($metadata['robots_index'], $metadata['robots_follow']) 263 | ->setAuthor($metadata['author']) 264 | ->setTitle($metadata['title']) 265 | ->setDescription($metadata['description']) 266 | ->setKeywords($metadata['keywords']) 267 | ->setOpenGraphType($metadata['og:type']) 268 | ->setInfotext($metadata['info']); 269 | 270 | if ($setCanonical == true) { 271 | 272 | if (!isset($metadata['params'])) { 273 | $params = Yii::$app->controller->actionParams; 274 | } else { 275 | $params = $metadata['params']; 276 | } 277 | 278 | if (!isset($metadata['route'])) { 279 | $params[0] = Yii::$app->controller->getRoute(); 280 | } else { 281 | $params[0] = $metadata['route']; 282 | } 283 | 284 | $url = Yii::$app->getUrlManager()->createAbsoluteUrl($params); 285 | 286 | if ($url !== Yii::$app->request->absoluteUrl) { 287 | $this->setCanonical($url); 288 | } 289 | 290 | $this->setOpenGraphUrl($url); 291 | } 292 | 293 | } 294 | } --------------------------------------------------------------------------------