Skip to content

Commit

Permalink
fixed component for modules && bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikalai Shershan committed Dec 16, 2015
1 parent fcfc24d commit ea55756
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 105 deletions.
100 changes: 0 additions & 100 deletions CustomView.php

This file was deleted.

13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,23 @@ in config file:
```sh
'components' => [
...
'view' => [
'class' => 'shershennm\seo\CustomView',
'controllerNamespace' => \\seo controllers namespace
'seo' => [
'class' => 'shershennm\seo\Seo',
'controllerMapping' => [
'app\controllers' => 'app\seo\controllers', \\controllers for seo module
],

],
...
'bootstrap' => ['log', 'seo'],
...
]
```
seo controller example:
```sh
<?php

namespace app\seoControllers;
namespace app\seo\controllers;

use Yii;
use shershennm\seo\SeoController;
Expand Down
132 changes: 132 additions & 0 deletions Seo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php

namespace shershennm\seo;

use Yii;
use yii\base\Component;
use yii\helpers\Html;
use shershennm\seo\Title;

/**
*
*/
class Seo extends Component
{
public
$controllerMapping,

$titleAppend,
$titlePrepend;

private
$_controller,
$_reflectionController;

public function getController()
{
if ($this->_controller === null)
{
$this->_controller = Yii::$app->controller;
}

return $this->_controller;
}

public function getReflectionController()
{
if ($this->_reflectionController === null)
{
$this->_reflectionController = $this->buildReflectionController();
}

return $this->_reflectionController;
}


public function init()
{
parent::init();

if (Yii::$app->view !== null)
{
Yii::$app->view->on(yii\web\View::EVENT_BEGIN_PAGE, [$this, 'eventSetMeta']);
}
}

public function eventSetMeta()
{
if ($this->isValidController() && $this->isSeoControllerInMapping() && $this->isSeoControllerExists())
{
$seoController = Yii::createObject($this->buildSeoControllerClassName());
$actionMethod = $this->controller->action->actionMethod;

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

$meta = $seoController->$actionMethod();
$this->addMeta($meta);

if ($seoController->title !== null)
{
Yii::$app->view->title = $this->buildTitle($seoController->title);
}
}
}
}

private function buildTitle($title)
{
$defaults = [
'defaultPrepend' => $this->titlePrepend,
'defaultAppend' => $this->titleAppend,
];

if(is_array($title)) {
$title = new Title(array_merge($title, $defaults));
}
else
{
$title = new Title(array_merge(['title' => $title], $defaults));
}

return $title->buildTitle();
}

private function addMeta($metaArray)
{
foreach ($metaArray as $meta) {
Yii::$app->view->metaTags[] = Html::tag('meta', '', $meta);
}
}

private function buildReflectionController()
{
return (new \ReflectionClass($this->controller));
}

private function isSeoControllerInMapping()
{
return isset($this->controllerMapping[$this->reflectionController->getNamespaceName()]);
}

private function isSeoControllerExists()
{
return class_exists($this->buildSeoControllerClassName());
}

private function isValidController()
{
return (Yii::$app->controller !== null && Yii::$app->controller->action->className() !== 'yii\web\ErrorAction');
}

private function buildSeoControllerClassName()
{
return sprintf('%s\%s', $this->controllerMapping[$this->reflectionController->getNamespaceName()], $this->reflectionController->getShortName());
}

private function buildActionFunction()
{
return sprintf('action%s', ucfirst($this->controller->action->id));
}
}
2 changes: 1 addition & 1 deletion 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": "1.0.3",
"version": "2.0.0",
"type": "yii2-extension",
"keywords": ["yii2", "seo", "keywords", "meta"],
"homepage": "https://github.com/shershennm/yii2-seo",
Expand Down

0 comments on commit ea55756

Please sign in to comment.