diff --git a/CustomView.php b/CustomView.php deleted file mode 100644 index dba5b0d..0000000 --- a/CustomView.php +++ /dev/null @@ -1,100 +0,0 @@ -_controller === null) - { - $this->_controller = Yii::$app->controller; - } - - return $this->_controller; - } - - public function init() - { - parent::init(); - $this->on(self::EVENT_BEGIN_PAGE, [$this, 'eventSetMeta']); - } - - public function eventSetMeta() - { - $controllerName = $this->buildControllerClass(); - $actionName = $this->buildActionFunction(); - - if (class_exists($controllerName)) - { - $object = new $controllerName; - - if (method_exists($object, $actionName)) - { - $object->controller = $this->controller; - $meta = $object->$actionName(); - - $this->addMeta($meta); - - if ($object->title !== null) - { - $this->title = $this->buildTitle($object->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) { - $this->metaTags[] = Html::tag('meta', '', $meta); - } - } - - private function buildControllerClass() - { - preg_match('/\\w*$/', $this->controller->className(), $result); - $className = $result[0]; - return sprintf('%s\%s', $this->controllerNamespace, $className); - } - - private function buildActionFunction() - { - return sprintf('action%s', ucfirst($this->controller->action->id)); - } -} \ No newline at end of file diff --git a/README.md b/README.md index eadf4af..eff283e 100644 --- a/README.md +++ b/README.md @@ -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 _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)); + } +} \ No newline at end of file diff --git a/composer.json b/composer.json index 9a423b3..2d1c7b0 100644 --- a/composer.json +++ b/composer.json @@ -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",