Commit 3d2e4c728a4d487537ff6c095b3318decb82cc2f
1 parent
fb8277be
set redirect to url manager
Showing
1 changed file
with
31 additions
and
15 deletions
Show diff stats
frontend/components/UrlManager.php
| ... | ... | @@ -3,11 +3,13 @@ |
| 3 | 3 | namespace frontend\components; |
| 4 | 4 | |
| 5 | 5 | use artbox\core\models\Alias; |
| 6 | + use artbox\core\models\Language; | |
| 6 | 7 | use artbox\core\services\Languages; |
| 7 | 8 | use yii\helpers\Json; |
| 8 | 9 | use yii\web\NotFoundHttpException; |
| 9 | 10 | use yii\web\Request; |
| 10 | 11 | |
| 12 | + | |
| 11 | 13 | /** |
| 12 | 14 | * Url manager extended to work with aliases and languages |
| 13 | 15 | * |
| ... | ... | @@ -69,8 +71,12 @@ |
| 69 | 71 | */ |
| 70 | 72 | public function parseRequest($request) |
| 71 | 73 | { |
| 72 | - // $this->checkRedirect($request->url); | |
| 74 | + $redirect = $this->checkRedirect($request->url); | |
| 75 | + | |
| 76 | + if ($redirect !== null) { | |
| 73 | 77 | |
| 78 | + \Yii::$app->response->redirect("/" . $redirect->value, 301); | |
| 79 | + } | |
| 74 | 80 | $request = $this->parseLanguage($request); |
| 75 | 81 | $path=$request->pathInfo; |
| 76 | 82 | |
| ... | ... | @@ -183,27 +189,37 @@ |
| 183 | 189 | |
| 184 | 190 | return $request; |
| 185 | 191 | } |
| 186 | - | |
| 192 | + | |
| 187 | 193 | /** |
| 188 | - * Looks for rule in table(column) `redirect.from` if findes - | |
| 194 | + * Looks for rule in table(column) | |
| 195 | + * `redirect.from` if findes - | |
| 189 | 196 | * redirects to `redirect.to` |
| 190 | 197 | * |
| 191 | 198 | * @param string $url |
| 192 | 199 | */ |
| 193 | 200 | protected function checkRedirect(string $url) |
| 194 | 201 | { |
| 195 | - // $redirect = Redirect::find() | |
| 196 | - // ->where( | |
| 197 | - // [ | |
| 198 | - // 'from' => $url, | |
| 199 | - // ] | |
| 200 | - // ) | |
| 201 | - // ->one(); | |
| 202 | - // | |
| 203 | - // if ($redirect) { | |
| 204 | - // \Yii::$app->response->redirect($redirect->to) | |
| 205 | - // ->send(); | |
| 206 | - // } | |
| 202 | + $url1 = parse_url($url); | |
| 203 | + | |
| 204 | + | |
| 205 | + $string = '{"0":"' . ltrim($url1['path'], "/") . '"'; | |
| 206 | + if (isset($url1['query'])) { | |
| 207 | + parse_str($url1['query'], $url1['query']); | |
| 208 | + $string .= (isset($url1['query']['id'])) | |
| 209 | + ? ',"id":' . $url1['query']['id'] | |
| 210 | + : ''; | |
| 211 | + } | |
| 212 | + | |
| 213 | + $string .= '}'; | |
| 214 | + $alias = Alias::find() | |
| 215 | + ->where(['route' => $string]) | |
| 216 | + ->one(); | |
| 217 | + | |
| 218 | + return $alias; | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 207 | 223 | } |
| 208 | 224 | |
| 209 | 225 | protected function invalidParams($requestParams){ | ... | ... |