Skip to content

Commit

Permalink
seo clear url
Browse files Browse the repository at this point in the history
  • Loading branch information
loveorigami committed Oct 10, 2017
1 parent b9b37f6 commit 6a15e79
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
26 changes: 25 additions & 1 deletion src/core/SeoHandler.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php

namespace lo\plugins\core;

use Yii;
use yii\web\NotFoundHttpException;
use yii\web\Application;

/**
Expand All @@ -13,15 +15,37 @@ class SeoHandler
{
/**
* Set page suffix
* Handler for yii\base\Application::beforeRequest
*/
public static function clearUrl()
{
$request = Yii::$app->request->url;

// Проверяем, если есть в урле index.php или ?r=, то кидаем 404 ошибку
if (
(strpos($request, 'index.php') !== false) ||
(strpos($request, '?r=') !== false) ||
(strpos($request, 'site') !== false) ||
(strpos($request, 'site/index') !== false)
) {
Yii::$app->response->redirect('/', 301);
}
}

/**
* Set page suffix
* Handler for yii\web\View::beginPage
*/
public static function updateTitle()
{
if (Yii::$app instanceof Application === true && Yii::$app->request->get('page') !== null) {
Yii::$app->view->title .= Yii::t(
'plugin',
' - Page {page}',
['page' => (int) Yii::$app->request->get('page')]
['page' => (int)Yii::$app->request->get('page')]
);
}
}


}
3 changes: 2 additions & 1 deletion src/migrations/m170105_094942_plugins_event.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ public function up()
'trigger_event' => 'beginPage',
'handler_class' => 'lo\plugins\core\SeoHandler',
'handler_method' => 'updateTitle',
'status' => self::EVENTS_ACTIVE
'status' => self::EVENTS_ACTIVE,
'pos' => 1
]);

$this->insert($this->tn(self::TBL_EVENT), [
Expand Down
29 changes: 29 additions & 0 deletions src/migrations/m170105_094960_seo_clear_url.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace lo\plugins\migrations;

class m170105_094960_seo_clear_url extends Migration
{

public function up()
{
$this->insert($this->tn(self::TBL_EVENT), [
'id' => 5,
'app_id' => self::APP_FRONTEND,
'plugin_id' => self::EVENTS_CORE, // Hello, world
'category_id' => self::CAT_SEO,
'trigger_class' => 'yii\base\Application',
'trigger_event' => 'beforeRequest',
'handler_class' => 'lo\plugins\core\SeoHandler',
'handler_method' => 'clearUrl',
'data' => '{}',
'status' => self::EVENTS_ACTIVE
]);
}

public function down()
{
$this->delete($this->tn(self::TBL_EVENT), ['id' => 5]);
}

}

0 comments on commit 6a15e79

Please sign in to comment.