Commit ff81af45d8c025feaafafd971e360e134f92b0a8
1 parent
298bc0f4
-Languages in process
Showing
12 changed files
with
214 additions
and
4 deletions
Show diff stats
console/migrations/m161101_142334_blog_article.php renamed to console/migrations/blog/m161101_142334_blog_article.php
console/migrations/m161101_142752_blog_article_lang.php renamed to console/migrations/blog/m161101_142752_blog_article_lang.php
console/migrations/m161101_143033_blog_category.php renamed to console/migrations/blog/m161101_143033_blog_category.php
console/migrations/m161101_143259_blog_category_lang.php renamed to console/migrations/blog/m161101_143259_blog_category_lang.php
console/migrations/m161101_143541_blog_article_to_category.php renamed to console/migrations/blog/m161101_143541_blog_article_to_category.php
console/migrations/m161101_143734_blog_tag.php renamed to console/migrations/blog/m161101_143734_blog_tag.php
console/migrations/m161101_143939_blog_tag_lang.php renamed to console/migrations/blog/m161101_143939_blog_tag_lang.php
console/migrations/m161101_144140_blog_article_to_tag.php renamed to console/migrations/blog/m161101_144140_blog_article_to_tag.php
console/migrations/m161101_144312_blog_article_to_article.php renamed to console/migrations/blog/m161101_144312_blog_article_to_article.php
| 1 | +<?php | ||
| 2 | + | ||
| 3 | + namespace frontend\components; | ||
| 4 | + | ||
| 5 | + use artbox\core\models\Alias; | ||
| 6 | + use artbox\core\services\Languages; | ||
| 7 | + use yii\helpers\Json; | ||
| 8 | + use yii\web\Request; | ||
| 9 | + | ||
| 10 | + /** | ||
| 11 | + * Url manager extended to work with aliases and languages | ||
| 12 | + * | ||
| 13 | + * @package artbox\core\seo | ||
| 14 | + */ | ||
| 15 | + class UrlManager extends \yii\web\UrlManager | ||
| 16 | + { | ||
| 17 | + /** | ||
| 18 | + * @var bool | ||
| 19 | + */ | ||
| 20 | + public $hideDefaultLanguagePrefix = false; | ||
| 21 | + | ||
| 22 | + /** | ||
| 23 | + * @see \yii\web\UrlManager | ||
| 24 | + * @var bool | ||
| 25 | + */ | ||
| 26 | + public $enablePrettyUrl = true; | ||
| 27 | + | ||
| 28 | + /** | ||
| 29 | + * @see \yii\web\UrlManager | ||
| 30 | + * @var bool | ||
| 31 | + */ | ||
| 32 | + public $showScriptName = false; | ||
| 33 | + | ||
| 34 | + /** | ||
| 35 | + * @var \artbox\core\services\Languages | ||
| 36 | + */ | ||
| 37 | + protected $languages; | ||
| 38 | + | ||
| 39 | + /** | ||
| 40 | + * UrlManager constructor. | ||
| 41 | + * | ||
| 42 | + * @param \artbox\core\services\Languages $languages | ||
| 43 | + * @param array $config | ||
| 44 | + */ | ||
| 45 | + public function __construct(Languages $languages, array $config = []) | ||
| 46 | + { | ||
| 47 | + $this->languages = $languages; | ||
| 48 | + | ||
| 49 | + parent::__construct($config); | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + /** | ||
| 53 | + * @param \yii\web\Request $request | ||
| 54 | + * | ||
| 55 | + * @return array|bool | ||
| 56 | + * @throws \artbox\core\exceptions\AliasOverwriteException | ||
| 57 | + * @throws \yii\base\InvalidConfigException | ||
| 58 | + */ | ||
| 59 | + public function parseRequest($request) | ||
| 60 | + { | ||
| 61 | + $this->checkRedirect($request->url); | ||
| 62 | + | ||
| 63 | + $request = $this->parseLanguage($request); | ||
| 64 | + /** | ||
| 65 | + * @var Alias $alias | ||
| 66 | + */ | ||
| 67 | + $alias = Alias::find() | ||
| 68 | + ->where( | ||
| 69 | + [ | ||
| 70 | + 'value' => trim($request->pathInfo, '/'), | ||
| 71 | + ] | ||
| 72 | + ) | ||
| 73 | + ->andWhere( | ||
| 74 | + [ | ||
| 75 | + 'language_id' => $this->languages->getCurrent()->id, | ||
| 76 | + ] | ||
| 77 | + ) | ||
| 78 | + ->one(); | ||
| 79 | + | ||
| 80 | + if ($alias !== NULL) { | ||
| 81 | + $params = Json::decode($alias->route); | ||
| 82 | + | ||
| 83 | + $route = array_shift($params); | ||
| 84 | + | ||
| 85 | + /** | ||
| 86 | + * @todo REFACTOR AS SOO AS POSIBLE! | ||
| 87 | + * remove service locator, and implement Dependency Injection | ||
| 88 | + * @var \artbox\core\components\SeoComponent $seo | ||
| 89 | + */ | ||
| 90 | + $seo = \Yii::$app->get('seo'); | ||
| 91 | + $seo->setAlias($alias); | ||
| 92 | + return [ | ||
| 93 | + $route, | ||
| 94 | + $params, | ||
| 95 | + ]; | ||
| 96 | + } | ||
| 97 | + | ||
| 98 | + return parent::parseRequest($request); | ||
| 99 | + } | ||
| 100 | + | ||
| 101 | + /** | ||
| 102 | + * @param array|string $params | ||
| 103 | + * | ||
| 104 | + * @return string | ||
| 105 | + */ | ||
| 106 | + public function createUrl($params) | ||
| 107 | + { | ||
| 108 | + if (isset($params[ 'alias' ])) { | ||
| 109 | + if ($params[ 'alias' ] instanceof Alias) { | ||
| 110 | + return '/' . $this->languages->getCurrent()->url . '/' . $params[ 'alias' ]->value; | ||
| 111 | + } elseif (is_array($params[ 'alias' ])) { | ||
| 112 | + return '/' . $this->languages->getCurrent()->url . '/' . $params[ 'alias' ][ 'value' ]; | ||
| 113 | + } | ||
| 114 | + } | ||
| 115 | + | ||
| 116 | + if ($this->hideDefaultLanguagePrefix && ( $this->languages->getCurrent( | ||
| 117 | + )->url == $this->languages->getDefault()->url )) { | ||
| 118 | + return parent::createUrl($params); | ||
| 119 | + } else { | ||
| 120 | + return '/' . $this->languages->getCurrent()->url . parent::createUrl($params); | ||
| 121 | + | ||
| 122 | + } | ||
| 123 | + } | ||
| 124 | + | ||
| 125 | + /** | ||
| 126 | + * @param Request $request | ||
| 127 | + * | ||
| 128 | + * @return Request | ||
| 129 | + * @throws \yii\base\InvalidConfigException | ||
| 130 | + */ | ||
| 131 | + protected function parseLanguage($request) | ||
| 132 | + { | ||
| 133 | + $split = explode('/', $request->pathInfo); | ||
| 134 | + if (in_array($split[ 0 ], array_keys($this->languages->getActive()))) { | ||
| 135 | + if ($this->hideDefaultLanguagePrefix && ( $split[ 0 ] == $this->languages->getDefault()->url )) { | ||
| 136 | + unset($split[ 0 ]); | ||
| 137 | + | ||
| 138 | + \Yii::$app->response->redirect('/' . implode('/', $split), 301) | ||
| 139 | + ->send(); | ||
| 140 | + } else { | ||
| 141 | + $this->languages->setCurrent($split[ 0 ]); | ||
| 142 | + | ||
| 143 | + unset($split[ 0 ]); | ||
| 144 | + | ||
| 145 | + $request->setPathInfo(implode('/', $split)); | ||
| 146 | + } | ||
| 147 | + } else { | ||
| 148 | + if ($this->hideDefaultLanguagePrefix) { | ||
| 149 | + $this->languages->setCurrentDefault(); | ||
| 150 | + } else { | ||
| 151 | + \Yii::$app->response->redirect( | ||
| 152 | + '/' . $this->languages->getDefault()->url . '/' . implode('/', $split), | ||
| 153 | + 301 | ||
| 154 | + ) | ||
| 155 | + ->send(); | ||
| 156 | + } | ||
| 157 | + } | ||
| 158 | + | ||
| 159 | + return $request; | ||
| 160 | + } | ||
| 161 | + | ||
| 162 | + /** | ||
| 163 | + * Looks for rule in table(column) `redirect.from` if findes - | ||
| 164 | + * redirects to `redirect.to` | ||
| 165 | + * | ||
| 166 | + * @param string $url | ||
| 167 | + */ | ||
| 168 | + protected function checkRedirect(string $url) | ||
| 169 | + { | ||
| 170 | + // $redirect = Redirect::find() | ||
| 171 | + // ->where( | ||
| 172 | + // [ | ||
| 173 | + // 'from' => $url, | ||
| 174 | + // ] | ||
| 175 | + // ) | ||
| 176 | + // ->one(); | ||
| 177 | + // | ||
| 178 | + // if ($redirect) { | ||
| 179 | + // \Yii::$app->response->redirect($redirect->to) | ||
| 180 | + // ->send(); | ||
| 181 | + // } | ||
| 182 | + } | ||
| 183 | + } | ||
| 0 | \ No newline at end of file | 184 | \ No newline at end of file |
frontend/config/main.php
| 1 | <?php | 1 | <?php |
| 2 | - use artbox\core\components\LanguageRequest; | ||
| 3 | use artbox\core\components\SeoComponent; | 2 | use artbox\core\components\SeoComponent; |
| 4 | - use artbox\core\components\SeoUrlManager; | ||
| 5 | - use artbox\core\seo\UrlManager; | 3 | + use frontend\components\UrlManager; |
| 6 | 4 | ||
| 7 | $params = array_merge( | 5 | $params = array_merge( |
| 8 | require( __DIR__ . '/../../common/config/params.php' ), | 6 | require( __DIR__ . '/../../common/config/params.php' ), |
| @@ -137,7 +135,6 @@ | @@ -137,7 +135,6 @@ | ||
| 137 | ], | 135 | ], |
| 138 | 'components' => [ | 136 | 'components' => [ |
| 139 | 'request' => [ | 137 | 'request' => [ |
| 140 | - 'class' => LanguageRequest::className(), | ||
| 141 | 'csrfParam' => '_csrf-frontend', | 138 | 'csrfParam' => '_csrf-frontend', |
| 142 | 'baseUrl' => '', | 139 | 'baseUrl' => '', |
| 143 | ], | 140 | ], |
| 1 | +<?php | ||
| 2 | + | ||
| 3 | + namespace frontend\widgets; | ||
| 4 | + | ||
| 5 | + use artbox\core\services\Languages; | ||
| 6 | + use yii\base\Widget; | ||
| 7 | + use yii\web\Request; | ||
| 8 | + | ||
| 9 | + class LanguageWidget extends Widget | ||
| 10 | + { | ||
| 11 | + protected $request; | ||
| 12 | + | ||
| 13 | + protected $languages; | ||
| 14 | + | ||
| 15 | + public function __construct(Languages $languages ,Request $request ,array $config = []) | ||
| 16 | + { | ||
| 17 | + parent::__construct($config); | ||
| 18 | + | ||
| 19 | + $this->request = $request; | ||
| 20 | + | ||
| 21 | + $this->languages = $languages; | ||
| 22 | + } | ||
| 23 | + | ||
| 24 | + public function init() | ||
| 25 | + { | ||
| 26 | + parent::init(); | ||
| 27 | + | ||
| 28 | +// $route = $this->request->get | ||
| 29 | + } | ||
| 30 | + } | ||
| 0 | \ No newline at end of file | 31 | \ No newline at end of file |