Commit 9315e305aaa336fd99173e18e00d70de8180812c
1 parent
4d77d202
- slash redirect
Showing
3 changed files
with
61 additions
and
17 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 | 4 | ||
5 | $params = array_merge( | 5 | $params = array_merge( |
6 | require( __DIR__ . '/../../common/config/params.php' ), | 6 | require( __DIR__ . '/../../common/config/params.php' ), |
@@ -10,21 +10,6 @@ use yii\helpers\Url; | @@ -10,21 +10,6 @@ use yii\helpers\Url; | ||
10 | ); | 10 | ); |
11 | 11 | ||
12 | return [ | 12 | return [ |
13 | - 'on beforeRequest' => function () { | ||
14 | - | ||
15 | - $pathInfo = Yii::$app->request->pathInfo; | ||
16 | - $query = Yii::$app->request->queryString; | ||
17 | - if (!empty($pathInfo) && substr($pathInfo, -1) === '/' && substr($pathInfo, 0, 1) !== "/") { | ||
18 | - $url = '/' . substr($pathInfo, 0, -1); | ||
19 | - if ($query) { | ||
20 | - $url .= '?' . $query; | ||
21 | - } | ||
22 | - Yii::$app->response->redirect($url, 301); | ||
23 | - } # редирект с www.siteName.com//////////////////////// на www.siteName.com | ||
24 | - elseif (substr($pathInfo, 0, 1) == "/" && substr($pathInfo, 1, 1) == "/") { | ||
25 | - Yii::$app->response->redirect('/', 301); | ||
26 | - } | ||
27 | - }, | ||
28 | 'id' => 'app-frontend', | 13 | 'id' => 'app-frontend', |
29 | 'homeUrl' => '/', | 14 | 'homeUrl' => '/', |
30 | 'basePath' => dirname(__DIR__), | 15 | 'basePath' => dirname(__DIR__), |
@@ -382,6 +367,9 @@ use yii\helpers\Url; | @@ -382,6 +367,9 @@ use yii\helpers\Url; | ||
382 | 367 | ||
383 | ], | 368 | ], |
384 | 'hideDefaultLanguagePrefix' => true, | 369 | 'hideDefaultLanguagePrefix' => true, |
370 | + 'redirects' => [ | ||
371 | + SlashRedirect::className(), | ||
372 | + ], | ||
385 | ], | 373 | ], |
386 | 'assetsAutoCompress' => [ | 374 | 'assetsAutoCompress' => [ |
387 | 'class' => '\skeeks\yii2\assetsAuto\AssetsAutoCompressComponent', | 375 | 'class' => '\skeeks\yii2\assetsAuto\AssetsAutoCompressComponent', |