-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRequest.php
70 lines (60 loc) · 2.57 KB
/
Request.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
<?php
/**
* @author Rytis Grincevicius <rytis@kiberzauras.com>
* @link http://www.github.com/kiberzauras/laravel.multilanguage
* @version 2.0.4
* @license MIT
*/
namespace Kiberzauras\MultiLanguage;
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
use Illuminate\Http\Request as LaravelRequest;
class Request extends LaravelRequest
{
/** There we will check if first segment of request uri is one our supported
* languages, then we will set it to locale and remove from request, this way
* there will be no need to configure our routes.php file for any of languages.
* @return LaravelRequest
*
* @todo If request doesn't have language, it would be great to check browser language or location.
*/
public static function capture()
{
$config_url = config_path() . DIRECTORY_SEPARATOR . 'multilanguage.json';
if (file_exists($config_url)) {
$params = json_decode(file_get_contents($config_url));
if (!empty($params->enabled)) {
if (array_key_exists('HTTP_X_ORIGINAL_URL', $_SERVER))
self::parseServerVars('HTTP_X_ORIGINAL_URL', $params);
elseif (array_key_exists('HTTP_X_REWRITE_URL', $_SERVER))
self::parseServerVars('HTTP_X_REWRITE_URL', $params);
elseif (array_key_exists('UNENCODED_URL', $_SERVER) && $_SERVER['IIS_WasUrlRewritten'] == 1)
self::parseServerVars('UNENCODED_URL', $params);
elseif (array_key_exists('REQUEST_URI', $_SERVER))
self::parseServerVars('REQUEST_URI', $params);
elseif (array_key_exists('ORIG_PATH_INFO', $_SERVER))
self::parseServerVars('ORIG_PATH_INFO', $params);
}
defined('Language') || define('Language', !empty($params->default) ? $params->default : 'en');
}
static::enableHttpMethodParameterOverride();
return static::createFromBase(SymfonyRequest::createFromGlobals());
}
/**
* @param $var
* @param $params
*/
protected static function parseServerVars($var, $params)
{
$uri = trim($_SERVER[$var], '/');
$lang = strstr($uri, '/', true);
if (in_array($lang, $params->enabled)) {
// for accessing /en/page/page
$_SERVER[$var] = strstr($uri, '/');
define('Language', $lang);
} elseif (in_array($uri, $params->enabled)) {
// for accessing /, /en, and /en/ pages
$_SERVER[$var] = '/';
define('Language', $uri);
}
}
}