1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Laravel MultiLang package. |
4
|
|
|
* |
5
|
|
|
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Longman\LaravelMultiLang\Middleware; |
12
|
|
|
|
13
|
|
|
use Carbon\Carbon; |
14
|
|
|
use Closure; |
15
|
|
|
use Illuminate\Foundation\Application; |
16
|
|
|
use Illuminate\Routing\Redirector; |
17
|
|
|
use Longman\LaravelMultiLang\MultiLang as MultiLangLib; |
18
|
|
|
|
19
|
|
|
class MultiLang |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Application. |
23
|
|
|
* |
24
|
|
|
* @var \Illuminate\Foundation\Application |
25
|
|
|
*/ |
26
|
|
|
protected $app; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Redirector. |
30
|
|
|
* |
31
|
|
|
* @var \Illuminate\Routing\Redirector |
32
|
|
|
*/ |
33
|
|
|
protected $redirector; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Multilang. |
37
|
|
|
* |
38
|
|
|
* @var \Longman\LaravelMultiLang\Multilang |
39
|
|
|
*/ |
40
|
|
|
protected $multilang; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* MultiLang constructor. |
44
|
|
|
* |
45
|
|
|
* @param \Illuminate\Foundation\Application $app |
46
|
|
|
* @param \Illuminate\Routing\Redirector $redirector |
47
|
|
|
* @param \Longman\LaravelMultiLang\MultiLang $multilang |
48
|
|
|
*/ |
49
|
5 |
|
public function __construct(Application $app, Redirector $redirector, MultiLangLib $multilang) |
50
|
|
|
{ |
51
|
5 |
|
$this->app = $app; |
52
|
5 |
|
$this->redirector = $redirector; |
53
|
5 |
|
$this->multilang = $multilang; |
54
|
5 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Handle an incoming request. |
58
|
|
|
* |
59
|
|
|
* @param \Illuminate\Http\Request $request |
60
|
|
|
* @param \Closure $next |
61
|
|
|
* @return mixed |
62
|
|
|
*/ |
63
|
5 |
|
public function handle($request, Closure $next) |
64
|
|
|
{ |
65
|
5 |
|
$url = $this->multilang->getRedirectUrl($request); |
66
|
|
|
|
67
|
5 |
|
if ($url !== null) { |
68
|
4 |
|
if ($request->expectsJson()) { |
69
|
1 |
|
return response('Not found', 404); |
70
|
|
|
} else { |
71
|
3 |
|
return $this->redirector->to($url); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
$locale = $this->multilang->detectLocale($request); |
76
|
|
|
|
77
|
1 |
|
$this->app->setLocale($locale); |
78
|
|
|
|
79
|
1 |
|
if ($this->multilang->getConfig()->get('set_carbon_locale')) { |
80
|
1 |
|
Carbon::setLocale($locale); |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
return $next($request); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|