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 | 37 | \ No newline at end of file | ... | ... |
frontend/components/UrlManager.php
... | ... | @@ -3,6 +3,7 @@ |
3 | 3 | namespace frontend\components; |
4 | 4 | |
5 | 5 | use artbox\core\models\Alias; |
6 | + use artbox\core\models\interfaces\RedirectInterface; | |
6 | 7 | use artbox\core\services\Languages; |
7 | 8 | use yii\helpers\Json; |
8 | 9 | use yii\web\Request; |
... | ... | @@ -42,6 +43,8 @@ |
42 | 43 | * @param \artbox\core\services\Languages $languages |
43 | 44 | * @param array $config |
44 | 45 | */ |
46 | + | |
47 | + public $redirects = []; | |
45 | 48 | public function __construct(Languages $languages, array $config = []) |
46 | 49 | { |
47 | 50 | $this->languages = $languages; |
... | ... | @@ -62,7 +65,13 @@ |
62 | 65 | // $this->checkRedirect($request->url); |
63 | 66 | |
64 | 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 | 76 | * @var Alias $alias |
68 | 77 | */ |
... | ... | @@ -210,4 +219,15 @@ |
210 | 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 | 234 | \ No newline at end of file | ... | ... |
frontend/config/main.php
1 | 1 | <?php |
2 | + use common\models\SlashRedirect; | |
2 | 3 | use frontend\components\UrlManager; |
3 | -use yii\helpers\Url; | |
4 | 4 | |
5 | 5 | $params = array_merge( |
6 | 6 | require( __DIR__ . '/../../common/config/params.php' ), |
... | ... | @@ -10,21 +10,6 @@ use yii\helpers\Url; |
10 | 10 | ); |
11 | 11 | |
12 | 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 | 13 | 'id' => 'app-frontend', |
29 | 14 | 'homeUrl' => '/', |
30 | 15 | 'basePath' => dirname(__DIR__), |
... | ... | @@ -382,6 +367,9 @@ use yii\helpers\Url; |
382 | 367 | |
383 | 368 | ], |
384 | 369 | 'hideDefaultLanguagePrefix' => true, |
370 | + 'redirects' => [ | |
371 | + SlashRedirect::className(), | |
372 | + ], | |
385 | 373 | ], |
386 | 374 | 'assetsAutoCompress' => [ |
387 | 375 | 'class' => '\skeeks\yii2\assetsAuto\AssetsAutoCompressComponent', | ... | ... |