Commit 2e6b55c4a0f5820e941bcaffc3772b4bc824ae8e
Merge remote-tracking branch 'origin/master'
# Conflicts: # frontend/config/main.php
Showing
3 changed files
with
61 additions
and
15 deletions
Show diff stats
| 1 | +<?php | ||
| 2 | + | ||
| 3 | + namespace common\models; | ||
| 4 | + | ||
| 5 | + use artbox\core\models\interfaces\RedirectInterface; | ||
| 6 | + use yii\base\BaseObject; | ||
| 7 | + | ||
| 8 | + class SlashRedirect extends BaseObject implements RedirectInterface | ||
| 9 | + { | ||
| 10 | + /** | ||
| 11 | + * @var string | ||
| 12 | + */ | ||
| 13 | + protected $link; | ||
| 14 | + | ||
| 15 | + /** | ||
| 16 | + * @param string $url | ||
| 17 | + * | ||
| 18 | + * @return bool | ||
| 19 | + */ | ||
| 20 | + public function doRedirect(string $url): bool | ||
| 21 | + { | ||
| 22 | + if (substr($url, -1) === '/') { | ||
| 23 | + $this->link = trim($url, '/'); | ||
| 24 | + return true; | ||
| 25 | + } | ||
| 26 | + return false; | ||
| 27 | + } | ||
| 28 | + | ||
| 29 | + /** | ||
| 30 | + * @return string | ||
| 31 | + */ | ||
| 32 | + public function getLink(): string | ||
| 33 | + { | ||
| 34 | + return $this->link; | ||
| 35 | + } | ||
| 36 | + } | ||
| 0 | \ No newline at end of file | 37 | \ No newline at end of file |
frontend/components/UrlManager.php
| @@ -3,6 +3,7 @@ | @@ -3,6 +3,7 @@ | ||
| 3 | namespace frontend\components; | 3 | namespace frontend\components; |
| 4 | 4 | ||
| 5 | use artbox\core\models\Alias; | 5 | use artbox\core\models\Alias; |
| 6 | + use artbox\core\models\interfaces\RedirectInterface; | ||
| 6 | use artbox\core\services\Languages; | 7 | use artbox\core\services\Languages; |
| 7 | use yii\helpers\Json; | 8 | use yii\helpers\Json; |
| 8 | use yii\web\Request; | 9 | use yii\web\Request; |
| @@ -42,6 +43,8 @@ | @@ -42,6 +43,8 @@ | ||
| 42 | * @param \artbox\core\services\Languages $languages | 43 | * @param \artbox\core\services\Languages $languages |
| 43 | * @param array $config | 44 | * @param array $config |
| 44 | */ | 45 | */ |
| 46 | + | ||
| 47 | + public $redirects = []; | ||
| 45 | public function __construct(Languages $languages, array $config = []) | 48 | public function __construct(Languages $languages, array $config = []) |
| 46 | { | 49 | { |
| 47 | $this->languages = $languages; | 50 | $this->languages = $languages; |
| @@ -62,7 +65,13 @@ | @@ -62,7 +65,13 @@ | ||
| 62 | // $this->checkRedirect($request->url); | 65 | // $this->checkRedirect($request->url); |
| 63 | 66 | ||
| 64 | $request = $this->parseLanguage($request); | 67 | $request = $this->parseLanguage($request); |
| 65 | - | 68 | + foreach ($this->redirects as $redirectClass) { |
| 69 | + /** | ||
| 70 | + * @var \artbox\core\models\interfaces\RedirectInterface $redirect | ||
| 71 | + */ | ||
| 72 | + $redirect = \Yii::createObject($redirectClass); | ||
| 73 | + $this->applyRedirect($redirect, $request->pathInfo); | ||
| 74 | + } | ||
| 66 | /** | 75 | /** |
| 67 | * @var Alias $alias | 76 | * @var Alias $alias |
| 68 | */ | 77 | */ |
| @@ -210,4 +219,15 @@ | @@ -210,4 +219,15 @@ | ||
| 210 | // ->send(); | 219 | // ->send(); |
| 211 | // } | 220 | // } |
| 212 | } | 221 | } |
| 222 | + | ||
| 223 | + public function applyRedirect(RedirectInterface $redirect, string $url) | ||
| 224 | + { | ||
| 225 | + if ($redirect->doRedirect($url)) { | ||
| 226 | + // var_dump($redirect->getLink());die(); | ||
| 227 | + header("HTTP/1.1 301 Moved Permanently"); | ||
| 228 | + header("Location: /" . $redirect->getLink()); | ||
| 229 | + exit(); | ||
| 230 | + } | ||
| 231 | + return 0; | ||
| 232 | + } | ||
| 213 | } | 233 | } |
| 214 | \ No newline at end of file | 234 | \ No newline at end of file |
frontend/config/main.php
| 1 | <?php | 1 | <?php |
| 2 | + use common\models\SlashRedirect; | ||
| 2 | use frontend\components\UrlManager; | 3 | use frontend\components\UrlManager; |
| 3 | use yii\helpers\Url; | 4 | use yii\helpers\Url; |
| 4 | 5 | ||
| @@ -10,20 +11,6 @@ use yii\helpers\Url; | @@ -10,20 +11,6 @@ use yii\helpers\Url; | ||
| 10 | ); | 11 | ); |
| 11 | 12 | ||
| 12 | return [ | 13 | return [ |
| 13 | - 'on beforeRequest' => function () { | ||
| 14 | - $pathInfo = Yii::$app->request->pathInfo; | ||
| 15 | - $query = Yii::$app->request->queryString; | ||
| 16 | - if (!empty($pathInfo) && substr($pathInfo, -1) === '/' && substr($pathInfo, 0, 1) !== "/") { | ||
| 17 | - $url = '/' . substr($pathInfo, 0, -1); | ||
| 18 | - if ($query) { | ||
| 19 | - $url .= '?' . $query; | ||
| 20 | - } | ||
| 21 | - Yii::$app->response->redirect($url, 301); | ||
| 22 | - } # редирект с www.siteName.com//////////////////////// на www.siteName.com | ||
| 23 | - elseif (substr($pathInfo, 0, 1) == "/" && substr($pathInfo, 1, 1) == "/") { | ||
| 24 | - Yii::$app->response->redirect('/', 301); | ||
| 25 | - } | ||
| 26 | - }, | ||
| 27 | 'id' => 'app-frontend', | 14 | 'id' => 'app-frontend', |
| 28 | 'homeUrl' => '/', | 15 | 'homeUrl' => '/', |
| 29 | 'basePath' => dirname(__DIR__), | 16 | 'basePath' => dirname(__DIR__), |
| @@ -397,6 +384,9 @@ use yii\helpers\Url; | @@ -397,6 +384,9 @@ use yii\helpers\Url; | ||
| 397 | 384 | ||
| 398 | ], | 385 | ], |
| 399 | 'hideDefaultLanguagePrefix' => true, | 386 | 'hideDefaultLanguagePrefix' => true, |
| 387 | + 'redirects' => [ | ||
| 388 | + SlashRedirect::className(), | ||
| 389 | + ], | ||
| 400 | ], | 390 | ], |
| 401 | 'assetsAutoCompress' => [ | 391 | 'assetsAutoCompress' => [ |
| 402 | 'class' => '\skeeks\yii2\assetsAuto\AssetsAutoCompressComponent', | 392 | 'class' => '\skeeks\yii2\assetsAuto\AssetsAutoCompressComponent', |