Commit 7164d2a700617423e5840cfccd8e969a0b049412
1 parent
7dfb61f2
1)Cделал редирект / в .htaccess
2) В страницах выводится seo->h1 если не присвоен свой заголовок в Page table
Showing
3 changed files
with
237 additions
and
234 deletions
Show diff stats
.htaccess
| ... | ... | @@ -4,6 +4,11 @@ AddDefaultCharset utf-8 |
| 4 | 4 | <IfModule mod_rewrite.c> |
| 5 | 5 | RewriteEngine On |
| 6 | 6 | |
| 7 | + | |
| 8 | + # Remove trailing slashes | |
| 9 | + RewriteCond %{REQUEST_FILENAME} !-d | |
| 10 | + RewriteRule ^(.*)/$ /$1 [L,R=301] | |
| 11 | + | |
| 7 | 12 | # the main rewrite rule for the frontend application |
| 8 | 13 | RewriteCond %{REQUEST_URI} !^/(backend/web|admin|storage) |
| 9 | 14 | RewriteRule !^frontend/web /frontend/web%{REQUEST_URI} [L] | ... | ... |
frontend/components/UrlManager.php
| 1 | 1 | <?php |
| 2 | - | |
| 3 | - namespace frontend\components; | |
| 4 | - | |
| 5 | - use artbox\core\models\Alias; | |
| 6 | - use artbox\core\models\Language; | |
| 7 | - use artbox\core\services\Languages; | |
| 8 | - use yii\helpers\Json; | |
| 9 | - use yii\web\NotFoundHttpException; | |
| 10 | - use yii\web\Request; | |
| 11 | - | |
| 12 | - | |
| 13 | - /** | |
| 14 | - * Url manager extended to work with aliases and languages | |
| 15 | - * | |
| 16 | - * @package artbox\core\seo | |
| 17 | - */ | |
| 18 | - class UrlManager extends \yii\web\UrlManager | |
| 19 | - { | |
| 20 | - /** | |
| 21 | - * @var bool | |
| 22 | - */ | |
| 23 | - public $hideDefaultLanguagePrefix = false; | |
| 24 | - | |
| 25 | - /** | |
| 26 | - * @see \yii\web\UrlManager | |
| 27 | - * @var bool | |
| 28 | - */ | |
| 29 | - public $enablePrettyUrl = true; | |
| 30 | - | |
| 31 | - /** | |
| 32 | - * @see \yii\web\UrlManager | |
| 33 | - * @var bool | |
| 34 | - */ | |
| 35 | - public $showScriptName = false; | |
| 36 | - | |
| 37 | - /** | |
| 38 | - * @var \artbox\core\services\Languages | |
| 39 | - */ | |
| 40 | - protected $languages; | |
| 41 | - | |
| 42 | - /** | |
| 43 | - * UrlManager constructor. | |
| 44 | - * | |
| 45 | - * @param \artbox\core\services\Languages $languages | |
| 46 | - * @param array $config | |
| 47 | - */ | |
| 48 | - | |
| 49 | - public $params = [ | |
| 50 | - 'id' => 'id', | |
| 51 | - 'page' => 'page', | |
| 52 | - 'per-page' => 'per_page', | |
| 53 | - 'book_id' => 'book_id', | |
| 54 | - 'q' => 'q', | |
| 55 | - 'tag' => 'tag', | |
| 56 | - ]; | |
| 57 | - public function __construct(Languages $languages, array $config = []) | |
| 58 | - { | |
| 59 | - $this->languages = $languages; | |
| 60 | - | |
| 61 | - parent::__construct($config); | |
| 62 | - } | |
| 63 | - | |
| 64 | - /** | |
| 65 | - * @param \yii\web\Request $request | |
| 66 | - * | |
| 67 | - * @return array|bool | |
| 68 | - * @throws \artbox\core\exceptions\AliasOverwriteException | |
| 69 | - * @throws \yii\base\ExitException | |
| 70 | - * @throws \yii\base\InvalidConfigException | |
| 71 | - */ | |
| 72 | - public function parseRequest($request) | |
| 73 | - { | |
| 74 | - $redirect = $this->checkRedirect($request->url); | |
| 75 | - | |
| 76 | - if ($redirect !== null) { | |
| 77 | - | |
| 78 | - \Yii::$app->response->redirect("/" . $redirect->value, 301); | |
| 79 | - } | |
| 80 | - $request = $this->parseLanguage($request); | |
| 81 | - $path=$request->pathInfo; | |
| 82 | - | |
| 83 | - if (strlen($path) && $path[strlen($path) - 1] == '/' | |
| 84 | - # && $path[strlen($path)-2]=='/' | |
| 85 | - ) | |
| 86 | - { | |
| 87 | - throw new NotFoundHttpException(); | |
| 88 | - } | |
| 89 | - | |
| 90 | - | |
| 91 | - /** | |
| 92 | - * @var Alias $alias | |
| 93 | - */ | |
| 94 | - $alias = Alias::find() | |
| 95 | - ->where( | |
| 96 | - [ | |
| 97 | - 'value' => trim($request->pathInfo, '/'), | |
| 98 | - ] | |
| 99 | - ) | |
| 100 | - ->andWhere( | |
| 101 | - [ | |
| 102 | - 'language_id' => $this->languages->getCurrent()->id, | |
| 103 | - ] | |
| 104 | - ) | |
| 105 | - ->one(); | |
| 106 | - $this->invalidParams(\Yii::$app->request->queryParams); | |
| 107 | - if ($alias !== null) { | |
| 108 | - $params = Json::decode($alias->route); | |
| 109 | - | |
| 110 | - $route = array_shift($params); | |
| 111 | - | |
| 112 | - /** | |
| 113 | - * @todo REFACTOR AS SOO AS POSIBLE! | |
| 114 | - * remove service locator, and implement Dependency Injection | |
| 115 | - * @var \artbox\core\components\SeoComponent $seo | |
| 116 | - */ | |
| 117 | - $seo = \Yii::$app->get('seo'); | |
| 118 | - $seo->setAlias($alias); | |
| 119 | - return [ | |
| 120 | - $route, | |
| 121 | - $params, | |
| 122 | - ]; | |
| 123 | - } | |
| 124 | - | |
| 125 | - return parent::parseRequest($request); | |
| 126 | - } | |
| 127 | - | |
| 128 | - /** | |
| 129 | - * @param array|string $params | |
| 130 | - * | |
| 131 | - * @return string | |
| 132 | - */ | |
| 133 | - public function createUrl($params) | |
| 134 | - { | |
| 135 | - if ($this->hideDefaultLanguagePrefix && ( $this->languages->getCurrent( | |
| 136 | - )->url == $this->languages->getDefault()->url )) { | |
| 137 | - $prefix = ''; | |
| 138 | - } else { | |
| 139 | - $prefix = '/' . $this->languages->getCurrent()->url; | |
| 140 | - } | |
| 141 | - | |
| 142 | - if (isset($params[ 'alias' ])) { | |
| 143 | - if ($params[ 'alias' ] instanceof Alias) { | |
| 144 | - return $prefix . '/' . $params[ 'alias' ]->value; | |
| 145 | - } elseif (is_array($params[ 'alias' ])) { | |
| 146 | - return $prefix . '/' . $params[ 'alias' ][ 'value' ]; | |
| 147 | - } | |
| 148 | - } | |
| 149 | - | |
| 150 | - return $prefix . parent::createUrl($params); | |
| 151 | - } | |
| 152 | - | |
| 153 | - /** | |
| 154 | - * @param $request | |
| 155 | - * | |
| 156 | - * @return mixed | |
| 157 | - * @throws \yii\base\ExitException | |
| 158 | - * @throws \yii\base\InvalidConfigException | |
| 159 | - */ | |
| 160 | - protected function parseLanguage(Request $request) | |
| 161 | - { | |
| 162 | - $split = explode('/', $request->pathInfo); | |
| 163 | - if (in_array($split[ 0 ], array_keys($this->languages->getActive()))) { | |
| 164 | - if ($this->hideDefaultLanguagePrefix && ( $split[ 0 ] == $this->languages->getDefault()->url )) { | |
| 165 | - unset($split[ 0 ]); | |
| 166 | - | |
| 167 | - \Yii::$app->response->redirect('/' . implode('/', $split), 301) | |
| 168 | - ->send(); | |
| 169 | - \Yii::$app->end(); | |
| 170 | - } else { | |
| 171 | - $this->languages->setCurrent($split[ 0 ]); | |
| 172 | - | |
| 173 | - unset($split[ 0 ]); | |
| 174 | - | |
| 175 | - $request->setPathInfo(implode('/', $split)); | |
| 176 | - } | |
| 177 | - } else { | |
| 178 | - if ($this->hideDefaultLanguagePrefix) { | |
| 179 | - $this->languages->setCurrentDefault(); | |
| 180 | - } else { | |
| 181 | - \Yii::$app->response->redirect( | |
| 182 | - '/' . $this->languages->getDefault()->url . '/' . implode('/', $split), | |
| 183 | - 301 | |
| 184 | - ) | |
| 185 | - ->send(); | |
| 186 | - \Yii::$app->end(); | |
| 187 | - } | |
| 188 | - } | |
| 189 | - | |
| 190 | - return $request; | |
| 191 | - } | |
| 192 | - | |
| 193 | - /** | |
| 194 | - * Looks for rule in table(column) | |
| 195 | - * `redirect.from` if findes - | |
| 196 | - * redirects to `redirect.to` | |
| 197 | - * | |
| 198 | - * @param string $url | |
| 199 | - */ | |
| 200 | - protected function checkRedirect(string $url) | |
| 201 | - { | |
| 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 | - | |
| 215 | - $alias = Alias::find() | |
| 216 | - ->where(['like', 'route', $string]) | |
| 217 | - ->limit(1) | |
| 218 | - ->one(); | |
| 219 | - return $alias; | |
| 220 | - | |
| 221 | - | |
| 222 | - | |
| 223 | - | |
| 224 | - } | |
| 225 | - | |
| 226 | - protected function invalidParams($requestParams){ | |
| 227 | - foreach ($requestParams as $key =>$param){ | |
| 228 | - | |
| 229 | - if (!array_key_exists($key, $this->params)){ | |
| 230 | - throw new NotFoundHttpException(); | |
| 231 | - } | |
| 232 | - } | |
| 233 | - } | |
| 234 | - } | |
| 235 | 2 | \ No newline at end of file |
| 3 | + | |
| 4 | +namespace frontend\components; | |
| 5 | + | |
| 6 | +use artbox\core\models\Alias; | |
| 7 | +use artbox\core\models\Language; | |
| 8 | +use artbox\core\services\Languages; | |
| 9 | +use yii\helpers\Json; | |
| 10 | +use yii\web\NotFoundHttpException; | |
| 11 | +use yii\web\Request; | |
| 12 | + | |
| 13 | + | |
| 14 | +/** | |
| 15 | + * Url manager extended to work with aliases and languages | |
| 16 | + * | |
| 17 | + * @package artbox\core\seo | |
| 18 | + */ | |
| 19 | +class UrlManager extends \yii\web\UrlManager | |
| 20 | +{ | |
| 21 | + /** | |
| 22 | + * @var bool | |
| 23 | + */ | |
| 24 | + public $hideDefaultLanguagePrefix = false; | |
| 25 | + | |
| 26 | + /** | |
| 27 | + * @see \yii\web\UrlManager | |
| 28 | + * @var bool | |
| 29 | + */ | |
| 30 | + public $enablePrettyUrl = true; | |
| 31 | + | |
| 32 | + /** | |
| 33 | + * @see \yii\web\UrlManager | |
| 34 | + * @var bool | |
| 35 | + */ | |
| 36 | + public $showScriptName = false; | |
| 37 | + | |
| 38 | + /** | |
| 39 | + * @var \artbox\core\services\Languages | |
| 40 | + */ | |
| 41 | + protected $languages; | |
| 42 | + | |
| 43 | + /** | |
| 44 | + * UrlManager constructor. | |
| 45 | + * | |
| 46 | + * @param \artbox\core\services\Languages $languages | |
| 47 | + * @param array $config | |
| 48 | + */ | |
| 49 | + | |
| 50 | + public $params = [ | |
| 51 | + 'id' => 'id', | |
| 52 | + 'page' => 'page', | |
| 53 | + 'per-page' => 'per_page', | |
| 54 | + 'book_id' => 'book_id', | |
| 55 | + 'q' => 'q', | |
| 56 | + 'tag' => 'tag', | |
| 57 | + ]; | |
| 58 | + | |
| 59 | + public function __construct(Languages $languages, array $config = []) | |
| 60 | + { | |
| 61 | + $this->languages = $languages; | |
| 62 | + | |
| 63 | + parent::__construct($config); | |
| 64 | + } | |
| 65 | + | |
| 66 | + /** | |
| 67 | + * @param \yii\web\Request $request | |
| 68 | + * | |
| 69 | + * @return array|bool | |
| 70 | + * @throws \artbox\core\exceptions\AliasOverwriteException | |
| 71 | + * @throws \yii\base\ExitException | |
| 72 | + * @throws \yii\base\InvalidConfigException | |
| 73 | + */ | |
| 74 | + public function parseRequest($request) | |
| 75 | + { | |
| 76 | + $redirect = $this->checkRedirect($request->url); | |
| 77 | + | |
| 78 | + if ($redirect !== null) { | |
| 79 | + | |
| 80 | + \Yii::$app->response->redirect("/" . $redirect->value, 301); | |
| 81 | + } | |
| 82 | + $request = $this->parseLanguage($request); | |
| 83 | + $path = $request->pathInfo; | |
| 84 | + | |
| 85 | + if (strlen($path) && $path[strlen($path) - 1] == '/' | |
| 86 | + # && $path[strlen($path)-2]=='/' | |
| 87 | + ) { | |
| 88 | + throw new NotFoundHttpException(); | |
| 89 | + } | |
| 90 | + | |
| 91 | + | |
| 92 | + /** | |
| 93 | + * @var Alias $alias | |
| 94 | + */ | |
| 95 | + $alias = Alias::find() | |
| 96 | + ->where( | |
| 97 | + [ | |
| 98 | + 'value' => trim($request->pathInfo, '/'), | |
| 99 | + ] | |
| 100 | + ) | |
| 101 | + ->andWhere( | |
| 102 | + [ | |
| 103 | + 'language_id' => $this->languages->getCurrent()->id, | |
| 104 | + ] | |
| 105 | + ) | |
| 106 | + ->one(); | |
| 107 | + $this->invalidParams(\Yii::$app->request->queryParams); | |
| 108 | + if ($alias !== null) { | |
| 109 | + $params = Json::decode($alias->route); | |
| 110 | + | |
| 111 | + $route = array_shift($params); | |
| 112 | + | |
| 113 | + /** | |
| 114 | + * @todo REFACTOR AS SOO AS POSIBLE! | |
| 115 | + * remove service locator, and implement Dependency Injection | |
| 116 | + * @var \artbox\core\components\SeoComponent $seo | |
| 117 | + */ | |
| 118 | + $seo = \Yii::$app->get('seo'); | |
| 119 | + $seo->setAlias($alias); | |
| 120 | + return [ | |
| 121 | + $route, | |
| 122 | + $params, | |
| 123 | + ]; | |
| 124 | + } | |
| 125 | + | |
| 126 | + return parent::parseRequest($request); | |
| 127 | + } | |
| 128 | + | |
| 129 | + /** | |
| 130 | + * @param array|string $params | |
| 131 | + * | |
| 132 | + * @return string | |
| 133 | + */ | |
| 134 | + public function createUrl($params) | |
| 135 | + { | |
| 136 | + if ($this->hideDefaultLanguagePrefix && ($this->languages->getCurrent()->url == $this->languages->getDefault()->url)) { | |
| 137 | + $prefix = ''; | |
| 138 | + } else { | |
| 139 | + $prefix = '/' . $this->languages->getCurrent()->url; | |
| 140 | + } | |
| 141 | + | |
| 142 | + if (isset($params['alias'])) { | |
| 143 | + if ($params['alias'] instanceof Alias) { | |
| 144 | + return $prefix . '/' . $params['alias']->value; | |
| 145 | + } elseif (is_array($params['alias'])) { | |
| 146 | + return $prefix . '/' . $params['alias']['value']; | |
| 147 | + } | |
| 148 | + } | |
| 149 | + | |
| 150 | + return $prefix . parent::createUrl($params); | |
| 151 | + } | |
| 152 | + | |
| 153 | + /** | |
| 154 | + * @param $request | |
| 155 | + * | |
| 156 | + * @return mixed | |
| 157 | + * @throws \yii\base\ExitException | |
| 158 | + * @throws \yii\base\InvalidConfigException | |
| 159 | + */ | |
| 160 | + protected function parseLanguage(Request $request) | |
| 161 | + { | |
| 162 | + $split = explode('/', $request->pathInfo); | |
| 163 | + if (in_array($split[0], array_keys($this->languages->getActive()))) { | |
| 164 | + if ($this->hideDefaultLanguagePrefix && ($split[0] == $this->languages->getDefault()->url)) { | |
| 165 | + unset($split[0]); | |
| 166 | + | |
| 167 | + \Yii::$app->response->redirect('/' . implode('/', $split), 301) | |
| 168 | + ->send(); | |
| 169 | + \Yii::$app->end(); | |
| 170 | + } else { | |
| 171 | + $this->languages->setCurrent($split[0]); | |
| 172 | + | |
| 173 | + unset($split[0]); | |
| 174 | + | |
| 175 | + $request->setPathInfo(implode('/', $split)); | |
| 176 | + } | |
| 177 | + } else { | |
| 178 | + if ($this->hideDefaultLanguagePrefix) { | |
| 179 | + $this->languages->setCurrentDefault(); | |
| 180 | + } else { | |
| 181 | + \Yii::$app->response->redirect( | |
| 182 | + '/' . $this->languages->getDefault()->url . '/' . implode('/', $split), | |
| 183 | + 301 | |
| 184 | + ) | |
| 185 | + ->send(); | |
| 186 | + \Yii::$app->end(); | |
| 187 | + } | |
| 188 | + } | |
| 189 | + | |
| 190 | + return $request; | |
| 191 | + } | |
| 192 | + | |
| 193 | + /** | |
| 194 | + * Looks for rule in table(column) | |
| 195 | + * `redirect.from` if findes - | |
| 196 | + * redirects to `redirect.to` | |
| 197 | + * | |
| 198 | + * @param string $url | |
| 199 | + */ | |
| 200 | + protected function checkRedirect(string $url) | |
| 201 | + { | |
| 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 | + | |
| 215 | + $alias = Alias::find() | |
| 216 | + ->where(['like', 'route', $string]) | |
| 217 | + ->limit(1) | |
| 218 | + ->one(); | |
| 219 | + return $alias; | |
| 220 | + | |
| 221 | + | |
| 222 | + } | |
| 223 | + | |
| 224 | + protected function invalidParams($requestParams) | |
| 225 | + { | |
| 226 | + foreach ($requestParams as $key => $param) { | |
| 227 | + | |
| 228 | + if (!array_key_exists($key, $this->params)) { | |
| 229 | + throw new NotFoundHttpException(); | |
| 230 | + } | |
| 231 | + } | |
| 232 | + } | |
| 233 | +} | |
| 236 | 234 | \ No newline at end of file | ... | ... |
frontend/views/site/about.php
| ... | ... | @@ -17,7 +17,7 @@ |
| 17 | 17 | <div class="row"> |
| 18 | 18 | <div class="col-xs-12 col-sm-12"> |
| 19 | 19 | |
| 20 | - <h1 class="card-desk-title"><?= ($page->language->title) ?? '' ?></h1> | |
| 20 | + <h1 class="card-desk-title"><?= ($page->language->title) ?? $seo->h1 ?></h1> | |
| 21 | 21 | </div> |
| 22 | 22 | <div class="col-xs-12 col-sm-12 card-desk-text blog-view"> |
| 23 | 23 | <?= ($page->language->body) ?? '' ?> | ... | ... |