Commit 3d2e4c728a4d487537ff6c095b3318decb82cc2f

Authored by alex
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,11 +3,13 @@
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\Language;
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\NotFoundHttpException; 9 use yii\web\NotFoundHttpException;
9 use yii\web\Request; 10 use yii\web\Request;
10 11
  12 +
11 /** 13 /**
12 * Url manager extended to work with aliases and languages 14 * Url manager extended to work with aliases and languages
13 * 15 *
@@ -69,8 +71,12 @@ @@ -69,8 +71,12 @@
69 */ 71 */
70 public function parseRequest($request) 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 $request = $this->parseLanguage($request); 80 $request = $this->parseLanguage($request);
75 $path=$request->pathInfo; 81 $path=$request->pathInfo;
76 82
@@ -183,27 +189,37 @@ @@ -183,27 +189,37 @@
183 189
184 return $request; 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 * redirects to `redirect.to` 196 * redirects to `redirect.to`
190 * 197 *
191 * @param string $url 198 * @param string $url
192 */ 199 */
193 protected function checkRedirect(string $url) 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 protected function invalidParams($requestParams){ 225 protected function invalidParams($requestParams){