项目作者: andrew72ru

项目描述 :
Add a description, keywords, etc. tags to Yii2 site page by url
高级语言: PHP
项目地址: git://github.com/andrew72ru/yii2-seo-tags.git
创建时间: 2017-02-13T11:14:50Z
项目社区:https://github.com/andrew72ru/yii2-seo-tags

开源协议:

下载


Build Status
Code Climate
Test Coverage
Issue Count

SEO tags module

Adds a description, keywords, etc. tags to site page by url

Installation

The preferred way to install this extension is through composer.

Add to you composer.json

  1. {
  2. "repositories": [
  3. {
  4. "type": "git",
  5. "url": "file:///Users/andrew/Sites/yii2-seo-tags"
  6. }
  7. ],
  8. }

Either run

  1. php composer.phar require --prefer-dist andrew72ru/yii2-seo-tags "*"

or add

  1. "andrew72ru/yii2-seo-tags": "*"

to the require section of your composer.json file.

Database migration class is andrew72ru\seotag\commands\m170301_051438_seotag.php. Add to you console application config

  1. 'controllerMap' => [
  2. 'migrate' => [
  3. 'class' => 'yii\console\controllers\MigrateController',
  4. 'migrationNamespaces' => [
  5. 'andrew72ru\seotag\commands',
  6. ],
  7. 'migrationPath' => null,
  8. ],
  9. ]

Settings

In you application config:

  1. 'modules' => [
  2. 'seotag' => [
  3. 'class' => 'andrew72ru\seotag\Module',
  4. 'urlManager' => '\yii\web\UrlManager',
  5. 'baseUrl' => 'http://you.site.url',
  6. 'twitterUsername' => '@you_twitter_username',
  7. 'imagePath' => '@webroot/assets/share',
  8. 'imageUrl' => '/share'
  9. ]
  10. ]
  • urlManager creates a addresses for target pages and pictures.
  • baseUrl need to create url to frontend from backend, for example
  • twitterUsername uses in meta-tag twitter:site. For example, with module twitterUsername setting is @you_twitter_username meta-tag will be the <meta property="twitter:site" content="@you_twitter_username">
  • imagePath, alias, where module will save a pictures for og:image meta-tag
  • imageUrl – relative url to image directory. E.g. with 'imageUrl' => '/share' and ‘baseUrl’ => ‘http://you.site.url‘, url will be a http://you.site.url/share/<model_id>/big.jpg

TIP

Add 'controllerMap' => ['main' => '\your\own\MainController'] to module config and rewrite MainController.php::actionPagesList function to load pages data.

Example function

  1. public function actionPagesList($q = null)
  2. {
  3. Yii::$app->response->format = Response::FORMAT_JSON;
  4. $out = [];
  5. $pages = StaticPage::find()
  6. ->limit(50)->asArray();
  7. if(!is_null($q))
  8. {
  9. $pages->andwhere(['like', 'slug', $q]);
  10. $pages->orWhere(['like', 'title', $q]);
  11. }
  12. foreach ($pages->all() as $page)
  13. {
  14. $out[] = [
  15. 'url' => $page['slug'],
  16. 'name' => $page['title'],
  17. 'value' => $page['title'] . ' (' . $page['slug'] . ')',
  18. 'route' => Yii::$app->urlManager->createAbsoluteUrl([$page['slug']]),
  19. ];
  20. }
  21. $blog = Blog::find()
  22. ->limit(50)->asArray();
  23. if(!is_null($q))
  24. {
  25. $blog->andFilterWhere(['like', 'slug', $q]);
  26. $blog->orFilterWhere(['like', 'title', $q]);
  27. }
  28. foreach ($blog->all() as $blogItem)
  29. {
  30. $out[] = [
  31. 'url' => '/blog/' . $blogItem['slug'],
  32. 'name' => $blogItem['title'],
  33. 'value' => $blogItem['title'] . ' (/blog/' . $blogItem['slug'] . ')',
  34. 'route' => Yii::$app->urlManager->createAbsoluteUrl(['/blog/' . $blogItem['slug']]),
  35. ];
  36. }
  37. return $out;
  38. }

Usage

In url you.site/seotag you may create the meta-tag for any page on you site. Just type in create form relative url of page, description, images urls and keywords and save it.

In head of you layout file (e.g. ffrontend/views/layouts/main.php):

  1. use andrew72ru\seotag\widgets\metaTags;
  2. metaTags::widget();

This widget renders a meta-tags:

  • description;
  • keywords;
  • og:description (same as description);
  • og:locale (ru_RU)
  • og:site_name (Yii::$app->name);
  • og:url (canonical page url);
  • og:type (website);
  • og:image;
  • twitter:card;
  • twitter:image;
  • twitter:site (NOT READY);
  • twitter:title (page title);
  • twitter:description (same as description)
  • link rel=”canonical” with page address

Tests

  • clone this repository,
  • cd to cloned folder,
  • type composer install,
  • build the tests with vendor/bin/codecept build
  • run tests with vendor/bin/codecept run