forked from codemix/yii2-localeurls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUrlManager.php
414 lines (383 loc) · 15.8 KB
/
UrlManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
<?php
namespace codemix\localeurls;
use Yii;
use yii\base\InvalidConfigException;
use yii\web\Cookie;
use yii\web\UrlManager as BaseUrlManager;
/**
* UrlManager
*
* This class extends Yii's UrlManager and adds features to detect the language from the URL
* or from browser settings transparently. It also can persist the language in the user session
* and optionally in a cookie. It also adds the language parameter to any created URL.
*/
class UrlManager extends BaseUrlManager
{
/**
* @var array list of available language codes. More specific patterns should come first, e.g. 'en_us'
* before 'en'. This can also contain mapping of <url_value> => <language>, e.g. 'english' => 'en'.
*/
public $languages = [];
/**
* @var bool whether to enable locale URL specific features
*/
public $enableLocaleUrls = true;
/**
* @var bool whether the default language should use an URL code like any other configured language.
*
* By default this is `false`, so for URLs without a language code the default language is assumed.
* In addition any request to an URL that contains the default language code will be redirected to
* the same URL without a language code. So if the default language is `fr` and a user requests
* `/fr/some/page` he gets redirected to `/some/page`. This way the persistet language can be reset
* to the default language.
*
* If this is `true`, then an URL that does not contain any language code will be redirected to the
* same URL with default language code. So if for example the default language is `fr`, then
* any request to `/some/page` will be redirected to `/fr/some/page`.
*
*/
public $enableDefaultLanguageUrlCode = false;
/**
* @var bool whether to detect the app language from the HTTP headers (i.e. browser settings).
* Default is `true`.
*/
public $enableLanguageDetection = true;
/**
* @var bool whether to store the detected language in session and (optionally) a cookie. If this
* is `true` (default) and a returning user tries to access any URL without a language prefix,
* he'll be redirected to the respective stored language URL (e.g. /some/page -> /fr/some/page).
*/
public $enableLanguagePersistence = true;
/**
* @var string the name of the session key that is used to store the language. Default is '_language'.
*/
public $languageSessionKey = '_language';
/**
* @var string the name of the language cookie. Default is '_language'.
*/
public $languageCookieName = '_language';
/**
* @var int number of seconds how long the language information should be stored in cookie,
* if `$enableLanguagePersistence` is true. Set to `false` to disable the language cookie completely.
* Default is 30 days.
*/
public $languageCookieDuration = 2592000;
/**
* @var array configuration options for the language cookie. Note that `$languageCookieName`
* and `$languageCookeDuration` will override any `name` and `expire` settings provided here.
*/
public $languageCookieOptions = [];
/**
* @var array list of route and URL regex patterns to ignore during language processing. The keys
* of the array are patterns for routes, the values are patterns for URLs. Route patterns are checked
* during URL creation. If a pattern matches, no language parameter will be added to the created URL.
* URL patterns are checked during processing incoming requests. If a pattern matches, the language
* processing will be skipped for that URL. Examples:
*
* ~~~php
* [
* '#^site/(login|register)#' => '#^(login|register)#'
* '#^api/#' => '#^api/#',
* ]
* ~~~
*/
public $ignoreLanguageUrlPatterns = [];
/**
* @var string the language that was initially set in the application configuration
*/
protected $_defaultLanguage;
/**
* @inheritdoc
*/
public $enablePrettyUrl = true;
/**
* @var string if a parameter with this name is passed to any `createUrl()` method, the created URL
* will use the language specified there. URLs created this way can be used to switch to a different
* language. If no such parameter is used, the currently detected application language is used.
*/
public $languageParam = 'language';
/**
* @var \yii\web\Request
*/
protected $_request;
/**
* @var bool whether locale URL was processed
*/
protected $_processed = false;
/**
* @inheritdoc
*/
public function init()
{
if ($this->enableLocaleUrls && $this->languages) {
if (!$this->enablePrettyUrl) {
throw new InvalidConfigException('Locale URL support requires enablePrettyUrl to be set to true.');
}
}
$this->_defaultLanguage = Yii::$app->language;
return parent::init();
}
/**
* @return string the `language` option that was initially set in the application config file,
* before it was modified by this component.
*/
public function getDefaultLanguage()
{
return $this->_defaultLanguage;
}
/**
* @inheritdoc
*/
public function parseRequest($request)
{
if ($this->enableLocaleUrls && $this->languages) {
$process = true;
if ($this->ignoreLanguageUrlPatterns) {
$pathInfo = $request->getPathInfo();
foreach ($this->ignoreLanguageUrlPatterns as $k => $pattern) {
if (preg_match($pattern, $pathInfo)) {
Yii::trace("Ignore pattern '$pattern' matches '$pathInfo.' Skipping language processing.", __METHOD__);
$process = false;
}
}
}
if ($process && !$this->_processed) {
$this->_processed = true;
$this->processLocaleUrl($request);
}
}
return parent::parseRequest($request);
}
/**
* @inheritdoc
*/
public function createUrl($params)
{
if ($this->ignoreLanguageUrlPatterns) {
$params = (array) $params;
$route = trim($params[0], '/');
foreach ($this->ignoreLanguageUrlPatterns as $pattern => $v) {
if (preg_match($pattern, $route)) {
return parent::createUrl($params);
}
}
}
if ($this->enableLocaleUrls && $this->languages) {
$params = (array) $params;
if (isset($params[$this->languageParam])) {
$language = $params[$this->languageParam];
unset($params[$this->languageParam]);
$languageRequired = true;
} else {
$language = Yii::$app->language;
$languageRequired = false;
}
$url = parent::createUrl($params);
// Unless a language was explicitely specified in the parameters we can return a URL without any prefix
// for the default language, if suffixes are disabled for the default language. In any other case we
// always add the suffix, e.g. to create "reset" URLs that explicitely contain the default language.
if (!$languageRequired && !$this->enableDefaultLanguageUrlCode && $language===$this->getDefaultLanguage()) {
return $url;
} else {
$key = array_search($language, $this->languages);
$base = $this->showScriptName ? $this->getScriptUrl() : $this->getBaseUrl();
$length = strlen($base);
if (is_string($key)) {
$language = $key;
}
$language = strtolower($language);
// Remove any trailing slashes unless one is configured as suffix
if ($this->suffix!=='/') {
if (count($params)!==1) {
$url = preg_replace('#/\?#', '?', $url);
} else {
$url = rtrim($url, '/');
}
}
return $length ? substr_replace($url, "$base/$language", 0, $length) : "/$language$url";
}
} else {
return parent::createUrl($params);
}
}
/**
* Checks for a language or locale parameter in the URL and rewrites the pathInfo if found.
* If no parameter is found it will try to detect the language from persistent storage (session /
* cookie) or from browser settings.
*
* @var \yii\web\Request $request
*/
protected function processLocaleUrl($request)
{
$this->_request = $request;
$pathInfo = $request->getPathInfo();
$parts = [];
foreach ($this->languages as $k => $v) {
$value = is_string($k) ? $k : $v;
if (substr($value, -2)==='-*') {
$lng = substr($value, 0, -2);
$parts[] = "$lng\-[a-z]{2,3}";
$parts[] = $lng;
} else {
$parts[] = $value;
}
}
$pattern = implode('|', $parts);
if (preg_match("#^($pattern)\b(/?)#i", $pathInfo, $m)) {
$request->setPathInfo(mb_substr($pathInfo, mb_strlen($m[1].$m[2])));
$code = $m[1];
if (isset($this->languages[$code])) {
// Replace alias with language code
$language = $this->languages[$code];
} else {
list($language,$country) = $this->matchCode($code);
if ($country!==null) {
if ($code==="$language-$country") {
$this->redirectToLanguage(strtolower($code));
} else {
$language = "$language-$country";
}
}
if ($language===null) {
$language = $code;
}
}
Yii::$app->language = $language;
Yii::trace("Language code found in URL. Setting application language to '$language'.", __METHOD__);
if ($this->enableLanguagePersistence) {
Yii::$app->session[$this->languageSessionKey] = $language;
Yii::trace("Persisting language '$language' in session.", __METHOD__);
if ($this->languageCookieDuration) {
$cookie = new Cookie(array_merge(
['httpOnly' => true],
$this->languageCookieOptions,
[
'name' => $this->languageCookieName,
'value' => $language,
'expire' => time() + (int) $this->languageCookieDuration,
]
));
Yii::$app->getResponse()->getCookies()->add($cookie);
Yii::trace("Persisting language '$language' in cookie.", __METHOD__);
}
}
// "Reset" case: We called e.g. /fr/demo/page so the persisted language was set back to "fr".
// Now we can redirect to the URL without language prefix, if default prefixes are disabled.
if (!$this->enableDefaultLanguageUrlCode && $language===$this->_defaultLanguage) {
$this->redirectToLanguage('');
}
} else {
$language = null;
if ($this->enableLanguagePersistence) {
$language = Yii::$app->session->get($this->languageSessionKey);
$language!==null && Yii::trace("Found persisted language '$language' in session.", __METHOD__);
if ($language===null) {
$language = $request->getCookies()->getValue($this->languageCookieName);
$language!==null && Yii::trace("Found persisted language '$language' in cookie.", __METHOD__);
}
}
if ($language===null && $this->enableLanguageDetection) {
foreach ($request->getAcceptableLanguages() as $acceptable) {
list($language,$country) = $this->matchCode($acceptable);
if ($language!==null) {
$language = $country===null ? $language : "$language-$country";
Yii::trace("Detected browser language '$language'.", __METHOD__);
break;
}
}
}
if ($language===null || $language===$this->_defaultLanguage) {
if (!$this->enableDefaultLanguageUrlCode) {
return;
} else {
$language = $this->_defaultLanguage;
}
}
// #35: Only redirect if a valid language was found
if ($this->matchCode($language)===[null, null]) {
return;
}
$key = array_search($language, $this->languages);
if ($key && is_string($key)) {
$language = $key;
}
$this->redirectToLanguage(strtolower($language));
}
}
/**
* Tests whether the given code matches any of the configured languages.
*
* If the code is a single language code, and matches either
*
* - an exact language as configured (ll)
* - a language with a country wildcard (ll-*)
*
* this language code is returned.
*
* If the code also contains a country code, and matches either
*
* - an exact language/country code as configured (ll-CC)
* - a language with a country wildcard (ll-*)
*
* the code with uppercase country is returned. If only the language part matches
* a configured language, that language is returned.
*
* @param string $code the code to match
* @return array of [language, country] where both can be null if no match
*/
protected function matchCode($code)
{
$language = $code;
$country = null;
$parts = explode('-', $code);
if (count($parts)===2) {
$language = $parts[0];
$country = strtoupper($parts[1]);
}
if (in_array($code, $this->languages)) {
return [$language, $country];
} elseif (
$country && in_array("$language-$country", $this->languages) ||
in_array("$language-*", $this->languages)
) {
return [$language, $country];
} elseif (in_array($language, $this->languages)) {
return [$language, null];
} else {
return [null, null];
}
}
/**
* Redirect to the current URL with given language code applied
*
* @param string $language the language code to add. Can also be empty to not add any language code.
*/
protected function redirectToLanguage($language)
{
$result = parent::parseRequest($this->_request);
if ($result === false) {
throw new \yii\web\NotFoundHttpException(Yii::t('yii', 'Page not found.'));
}
list ($route, $params) = $result;
if($language){
$params[$this->languageParam] = $language;
}
// See Yii Issues #8291 and #9161:
$params = $params + $this->_request->getQueryParams();
array_unshift($params, $route);
$url = $this->createUrl($params);
// Required to prevent double slashes on generated URLs
if ($this->suffix==='/' && $route==='') {
$url = rtrim($url, '/').'/';
}
Yii::trace("Redirecting to $url.", __METHOD__);
Yii::$app->getResponse()->redirect($url);
if (YII_ENV_TEST) {
// Response::redirect($url) above will call `Url::to()` internally. So to really
// test for the same final redirect URL here, we need to call Url::to(), too.
throw new \yii\base\Exception(\yii\helpers\Url::to($url));
} else {
Yii::$app->end();
}
}
}