|
1
|
|
|
<?php namespace App\Services\Routing; |
|
2
|
|
|
|
|
3
|
|
|
use Illuminate\Routing\RouteCollection; |
|
4
|
|
|
|
|
5
|
|
|
class LocalizedRouteCollection extends RouteCollection |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* Add the route to any look-up tables if necessary. |
|
9
|
|
|
* |
|
10
|
|
|
* @param \Illuminate\Routing\Route $route |
|
11
|
|
|
* @return void |
|
12
|
|
|
*/ |
|
13
|
19 |
|
protected function addLookups($route) |
|
14
|
|
|
{ |
|
15
|
19 |
|
$locale = app('translator')->getLocale(); |
|
16
|
|
|
|
|
17
|
|
|
// If the route has a name, we will add it to the name look-up table so that we |
|
18
|
|
|
// will quickly be able to find any route associate with a name and not have |
|
19
|
|
|
// to iterate through every route every time we need to perform a look-up. |
|
20
|
19 |
|
$action = $route->getAction(); |
|
21
|
|
|
|
|
22
|
19 |
|
if (isset($action['as'])) { |
|
23
|
19 |
|
$this->nameList[$action['as']][$locale] = $route; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
// When the route is routing to a controller we will also store the action that |
|
27
|
|
|
// is used by the route. This will let us reverse route to controllers while |
|
28
|
|
|
// processing a request and easily generate URLs to the given controllers. |
|
29
|
19 |
|
if (isset($action['controller'])) { |
|
30
|
19 |
|
$this->addToActionList($action, $route); |
|
31
|
|
|
} |
|
32
|
19 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Add a route to the controller action dictionary. |
|
36
|
|
|
* |
|
37
|
|
|
* @param array $action |
|
38
|
|
|
* @param \Illuminate\Routing\Route $route |
|
39
|
|
|
* |
|
40
|
|
|
* @return void |
|
41
|
|
|
*/ |
|
42
|
19 |
|
protected function addToActionList($action, $route) |
|
43
|
|
|
{ |
|
44
|
19 |
|
$locale = app('translator')->getLocale(); |
|
45
|
19 |
|
$this->actionList[trim($action['controller'], '\\')][$locale] = $route; |
|
46
|
19 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Get a route instance by its controller action. |
|
50
|
|
|
* |
|
51
|
|
|
* @param string $action |
|
52
|
|
|
* |
|
53
|
|
|
* @return \Illuminate\Routing\Route|null |
|
54
|
|
|
*/ |
|
55
|
4 |
|
public function getByAction($action) |
|
56
|
|
|
{ |
|
57
|
4 |
|
$locale = app('translator')->getLocale(); |
|
58
|
|
|
|
|
59
|
4 |
|
return isset($this->actionList[$action][$locale]) ? $this->actionList[$action][$locale] : null; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Get a route instance by its name. |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $name |
|
66
|
|
|
* @return \Illuminate\Routing\Route|null |
|
67
|
|
|
*/ |
|
68
|
4 |
|
public function getByName($name) |
|
69
|
|
|
{ |
|
70
|
4 |
|
$locale = app('translator')->getLocale(); |
|
71
|
|
|
|
|
72
|
4 |
|
if (isset($this->nameList[$name])) { |
|
73
|
4 |
|
return $this->nameList[$name]; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return isset($this->nameList[$locale . '.' . $name]) ? $this->nameList[$locale . '.' . $name] : null; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|