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