Skip to content

Commit

Permalink
Merge pull request #2 from shershennm/staging
Browse files Browse the repository at this point in the history
3.1.0 Update
  • Loading branch information
shershennm authored May 10, 2018
2 parents a071474 + 3a4db76 commit ec4f561
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 50 deletions.
50 changes: 50 additions & 0 deletions OnePageSeoController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace shershennm\seo;

use Yii;

abstract class OnePageSeoController extends SeoController
{
/**
* @var array Array of routes with titles
*/
protected $titles = [];

/**
* @var array Array of routes with titles. Routes are regular expressions in this case
*/
protected $wildcardTitles = [];

/**
* Default action for one page controller.
*
* @return array
*/
public function actionIndex()
{
$this->setTitle(Yii::$app->request->pathInfo);

return [];
}

/**
* @param $route
*/
protected function setTitle($route)
{
if (isset($this->titles[$route])) {
$this->title = $this->titles[$route];

return;
}

foreach ($this->wildcardTitles as $wildcardRoute => $wildcardTitle) {
if (preg_match($wildcardRoute, $route)) {
$this->title = $wildcardTitle;

return;
}
}
}
}
43 changes: 28 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
# yii2-seo

Yii2 module for easy creating meta tags
Yii2 module for easy creating meta tags

# Installation

Yii <= 2.0.12

`composer require shershennm/yii2-seo:"^2.0"`

Yii >= 2.0.13

`composer require shershennm/yii2-seo:"^3.0"`
# Usage
in config file:
```sh
In config file:
```php
<?php
...
'bootstrap' => ['log', 'seo'], // add seo component to application bootstrap
...
Expand All @@ -28,8 +22,8 @@ in config file:
],
]
```
seo controller example:
```sh
Seo controller example:
```php
<?php

namespace app\seo\controllers;
Expand All @@ -38,7 +32,7 @@ use Yii;
use shershennm\seo\SeoController;

class SiteController extends SeoController
{
{
/**
* $viewParams array View Params from actionIndex in SiteController
**/
Expand All @@ -54,12 +48,31 @@ class SiteController extends SeoController
['name' => 'description', 'content' => 'Cool page!'],
];
}

private function getKeywords()
{
// $this->controller instance of current controller
return implode($this->controller->words, ', ');
}

....
```
```
You can use ```OnePagSeoController``` for controller which have ```index``` action for different routes. Example:
```php
<?php

namespace frontend\seo\controllers;

use shershennm\seo\OnePageSeoController;

class SiteController extends OnePageSeoController
{
protected $titles = [
'site/info' => 'Site Info',
];
protected $wildcardTitles = [
'/site\/history/' => 'Site History',
];
}
```
Route of ```$titles``` will be applied only to pages with same route. ```$wildcardTitles``` use regular expression as route.
31 changes: 14 additions & 17 deletions Seo.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace shershennm\seo;

use Yii;

use yii\base\ActionEvent;
use yii\base\Component;
use yii\base\ViewEvent;
Expand All @@ -13,7 +12,8 @@
class Seo extends Component
{
/**
* controller namespace => seo controller namespace
* controller namespace => seo controller namespace.
*
* @var array
*/
public $controllerMapping;
Expand Down Expand Up @@ -51,16 +51,15 @@ public function getController()
*/
public function getReflectionController()
{
if ($this->_reflectionController === null)
{
if ($this->_reflectionController === null) {
$this->_reflectionController = $this->buildReflectionController();
}

return $this->_reflectionController;
}

/**
* @inheritdoc
* {@inheritdoc}
*/
public function init()
{
Expand All @@ -69,17 +68,13 @@ public function init()
parent::init();
}

/**
* @return void
*/
private function initEventBranches()
{
ActionEvent::on(Controller::className(), Controller::EVENT_BEFORE_ACTION, [$this, 'eventControllerBeforeAction']);
}

/**
* @param $event ActionEvent
* @return void
*/
public function eventControllerBeforeAction($event)
{
Expand All @@ -89,7 +84,7 @@ public function eventControllerBeforeAction($event)

/**
* @param $event ViewEvent
* @return void
*
* @throws \yii\base\InvalidConfigException
*/
public function eventViewBeforeRender($event)
Expand All @@ -101,6 +96,7 @@ public function eventViewBeforeRender($event)

/**
* @param $viewEvent ViewEvent
*
* @throws \yii\base\InvalidConfigException
*/
public function setMeta($viewEvent)
Expand All @@ -118,16 +114,17 @@ public function setMeta($viewEvent)

/**
* @param $viewEvent ViewEvent
*
* @return bool
*
* @throws \yii\base\InvalidConfigException
*/
private function executeSeoControllerAction($viewEvent)
{
$seoController = Yii::createObject($this->buildSeoControllerClassName());
$actionMethod = $this->controller->action->actionMethod;

if (method_exists($seoController, $actionMethod))
{
if (method_exists($seoController, $actionMethod)) {
$seoController->controller = $this->controller;
$seoController->view = $viewEvent->sender;

Expand All @@ -144,6 +141,7 @@ private function executeSeoControllerAction($viewEvent)

/**
* @param $title string
*
* @return string
*/
protected function buildTitle($title)
Expand All @@ -154,7 +152,7 @@ protected function buildTitle($title)
'defaultAppend' => $this->titleAppend,
];

if(is_array($title)) {
if (is_array($title)) {
$title = new Title(array_merge($title, $defaults));
} else {
$title = new Title(array_merge(['title' => $title], $defaults));
Expand All @@ -166,7 +164,6 @@ protected function buildTitle($title)
/**
* @param $view View
* @param $metaArray array
* @return void
*/
public function addMeta($view, $metaArray)
{
Expand All @@ -189,7 +186,7 @@ public function addTitle($view, $title = null)
*/
private function buildReflectionController()
{
return (new \ReflectionClass($this->controller));
return new \ReflectionClass($this->controller);
}

/**
Expand All @@ -213,7 +210,7 @@ private function isSeoControllerExists()
*/
private function isValidController()
{
return (Yii::$app->controller !== null && Yii::$app->controller->action->className() !== 'yii\web\ErrorAction');
return Yii::$app->controller !== null && Yii::$app->controller->action->className() !== 'yii\web\ErrorAction';
}

/**
Expand All @@ -223,4 +220,4 @@ private function buildSeoControllerClassName()
{
return sprintf('%s\%s', $this->controllerMapping[$this->reflectionController->getNamespaceName()], $this->reflectionController->getShortName());
}
}
}
22 changes: 12 additions & 10 deletions SeoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,39 @@
abstract class SeoController extends BaseObject
{
/**
* @var $title string Page <title> tag value
* @var string Page <title> tag value
*/
public $title;
public $title;

/**
* @var $controller \yii\web\Controller Web Controller instance
* @var \yii\web\Controller Web Controller instance
*/
public $controller;
public $controller;

/**
* @var $view \yii\web\View Controller View
* @var \yii\web\View Controller View
*/
public $view;

/**
* Register <meta> tag in current view
* @param array $options params for View::registerMetaTag method
* Register <meta> tag in current view.
*
* @param array $options params for View::registerMetaTag method
*/
public function registerMetaTag($options)
{
return $this->view->registerMetaTag($options);
}

/**
* Register <link> tag in current view
* @param array $options params for View::registerLinkTag method
* Register <link> tag in current view.
*
* @param array $options params for View::registerLinkTag method
*/
public function registerLinkTag($options)
{
return $this->view->registerLinkTag(array_merge([
'type' => null,
], $options));
}
}
}
9 changes: 3 additions & 6 deletions Title.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,8 @@ public function buildTitle()
*/
public function getTitle()
{
if ($this->title === null)
{
if ($this->defaultTitle !== null)
{
if ($this->title === null) {
if ($this->defaultTitle !== null) {
return $this->defaultTitle;
}

Expand Down Expand Up @@ -89,5 +87,4 @@ public function getAppend()

return $this->append;
}

}
}
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "shershennm/yii2-seo",
"description": "Yii2 extension for simple generating keywords and description",
"version": "3.0.0",
"version": "3.1.0",
"type": "yii2-extension",
"keywords": ["yii2", "seo", "keywords", "meta", "link"],
"homepage": "https://github.com/shershennm/yii2-seo",
Expand All @@ -26,4 +26,4 @@
"shershennm\\seo\\": ""
}
}
}
}

0 comments on commit ec4f561

Please sign in to comment.