Blame view

components/LanguageUrlManager.php 1.41 KB
08453431   Yarik   first commit
1
2
3
4
  <?php
      namespace artweb\artbox\language\components;
      
      use artweb\artbox\language\models\Language;
6cd4f8ab   Alexey Boroda   -Bad get params 4...
5
      use yii\web\NotFoundHttpException;
08453431   Yarik   first commit
6
7
8
9
      use yii\web\UrlManager;
      
      class LanguageUrlManager extends UrlManager
      {
6cd4f8ab   Alexey Boroda   -Bad get params 4...
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
          /**
           * @var array
           */
          public $badGetParams = [];
          
          public function parseRequest($request)
          {
              foreach ($request->get() as $param => $value) {
                  if (in_array($param, $this->badGetParams)) {
                      throw new NotFoundHttpException();
                  }
              }
              
              return parent::parseRequest($request);
          }
08453431   Yarik   first commit
25
26
27
28
29
30
          
          /**
           * @inheritdoc
           */
          public function createUrl($params)
          {
6cd4f8ab   Alexey Boroda   -Bad get params 4...
31
              if (isset($params[ 'language_id' ])) {
08453431   Yarik   first commit
32
33
                  
                  $language = Language::findOne($params[ 'language_id' ]);
6cd4f8ab   Alexey Boroda   -Bad get params 4...
34
                  if ($language === null) {
08453431   Yarik   first commit
35
36
                      $language = Language::getDefaultLanguage();
                  }
6cd4f8ab   Alexey Boroda   -Bad get params 4...
37
                  unset($params[ 'language_id' ]);
08453431   Yarik   first commit
38
39
40
41
42
43
44
              } else {
                  
                  $language = Language::getCurrent();
              }
              
              $url = parent::createUrl($params);
              
6cd4f8ab   Alexey Boroda   -Bad get params 4...
45
              if ($url == '/') {
08453431   Yarik   first commit
46
47
48
49
50
                  return '/' . $language->url;
              } else {
                  return '/' . $language->url . $url;
              }
          }
08453431   Yarik   first commit
51
      }