1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CaribouFute\LocaleRoute\Prefix; |
4
|
|
|
|
5
|
|
|
use App; |
6
|
|
|
use CaribouFute\LocaleRoute\Prefix\Base; |
7
|
|
|
use Illuminate\Routing\Router as IlluminateRouter; |
8
|
|
|
use Illuminate\Routing\UrlGenerator; |
9
|
|
|
|
10
|
|
|
class Route extends Base |
11
|
|
|
{ |
12
|
|
|
protected $separator = '.'; |
13
|
|
|
protected $url; |
14
|
|
|
protected $router; |
15
|
|
|
|
16
|
46 |
|
public function __construct(UrlGenerator $url, IlluminateRouter $router) |
17
|
|
|
{ |
18
|
46 |
|
$this->url = $url; |
19
|
46 |
|
$this->router = $router; |
20
|
46 |
|
} |
21
|
|
|
|
22
|
8 |
|
public function localeRoute($locale = null, $name = null, $parameters = [], $absolute = true) |
23
|
|
|
{ |
24
|
8 |
|
$locale = $locale ?: App::getLocale(); |
25
|
8 |
|
$name = $name ?: $this->getCurrentRouteName(); |
26
|
|
|
|
27
|
8 |
|
$localeRoute = $this->switchLocale($locale, $name); |
28
|
8 |
|
$localeUrl = $this->url->route($localeRoute, $parameters, $absolute); |
29
|
8 |
|
$localeUrl = rtrim($localeUrl, '?'); |
30
|
|
|
|
31
|
8 |
|
return $localeUrl; |
32
|
|
|
} |
33
|
|
|
|
34
|
3 |
|
public function getCurrentRouteName() |
35
|
|
|
{ |
36
|
3 |
|
$currentRoute = $this->router->current(); |
37
|
3 |
|
$currentRouteName = $currentRoute ? $currentRoute->getName() : ''; |
38
|
|
|
|
39
|
3 |
|
return $currentRouteName; |
40
|
|
|
} |
41
|
|
|
|
42
|
1 |
|
public function otherLocale($locale = null, $parameters = null, $absolute = true) |
43
|
|
|
{ |
44
|
1 |
|
$name = $this->getCurrentRouteName(); |
45
|
1 |
|
$parameters = $parameters ?: $this->getCurrentRouteParameters(); |
46
|
|
|
|
47
|
1 |
|
return $this->localeRoute($locale, $name, $parameters, $absolute); |
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
|
public function getCurrentRouteParameters() |
51
|
|
|
{ |
52
|
1 |
|
$currentRoute = $this->router->current(); |
53
|
1 |
|
$currentRouteParameters = $currentRoute ? $currentRoute->parameters() : []; |
54
|
|
|
|
55
|
1 |
|
return $currentRouteParameters; |
56
|
|
|
} |
57
|
|
|
|
58
|
2 |
|
public function otherRoute($name, $parameters = null, $absolute = true) |
59
|
|
|
{ |
60
|
2 |
|
return $this->localeRoute(null, $name, $parameters, $absolute); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|