1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD; |
4
|
|
|
|
5
|
|
|
use Route; |
6
|
|
|
|
7
|
|
|
class CrudRouter |
8
|
|
|
{ |
9
|
|
|
protected $extraRoutes = []; |
10
|
|
|
|
11
|
|
|
protected $name = null; |
12
|
|
|
protected $options = null; |
13
|
|
|
protected $controller = null; |
14
|
|
|
|
15
|
|
|
public function __construct($name, $controller, $options) |
16
|
|
|
{ |
17
|
|
|
$this->name = $name; |
18
|
|
|
$this->controller = $controller; |
19
|
|
|
$this->options = $options; |
20
|
|
|
|
21
|
|
|
// CRUD routes for core features |
22
|
|
|
Route::post($this->name.'/search', [ |
23
|
|
|
'as' => 'crud.'.$this->name.'.search', |
24
|
|
|
'uses' => $this->controller.'@search', |
25
|
|
|
]); |
26
|
|
|
|
27
|
|
|
Route::get($this->name.'/reorder', [ |
28
|
|
|
'as' => 'crud.'.$this->name.'.reorder', |
29
|
|
|
'uses' => $this->controller.'@reorder', |
30
|
|
|
]); |
31
|
|
|
|
32
|
|
|
Route::post($this->name.'/reorder', [ |
33
|
|
|
'as' => 'crud.'.$this->name.'.save.reorder', |
34
|
|
|
'uses' => $this->controller.'@saveReorder', |
35
|
|
|
]); |
36
|
|
|
|
37
|
|
|
Route::get($this->name.'/{id}/details', [ |
38
|
|
|
'as' => 'crud.'.$this->name.'.showDetailsRow', |
39
|
|
|
'uses' => $this->controller.'@showDetailsRow', |
40
|
|
|
]); |
41
|
|
|
|
42
|
|
|
Route::get($this->name.'/{id}/translate/{lang}', [ |
43
|
|
|
'as' => 'crud.'.$this->name.'.translateItem', |
44
|
|
|
'uses' => $this->controller.'@translateItem', |
45
|
|
|
]); |
46
|
|
|
|
47
|
|
|
Route::get($this->name.'/{id}/revisions', [ |
48
|
|
|
'as' => 'crud.'.$this->name.'.listRevisions', |
49
|
|
|
'uses' => $this->controller.'@listRevisions', |
50
|
|
|
]); |
51
|
|
|
|
52
|
|
|
Route::post($this->name.'/{id}/revisions/{revisionId}/restore', [ |
53
|
|
|
'as' => 'crud.'.$this->name.'.restoreRevision', |
54
|
|
|
'uses' => $this->controller.'@restoreRevision', |
55
|
|
|
]); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* The CRUD resource needs to be registered after all the other routes. |
60
|
|
|
*/ |
61
|
|
|
public function __destruct() |
62
|
|
|
{ |
63
|
|
|
$options_with_default_route_names = array_merge([ |
64
|
|
|
'names' => [ |
65
|
|
|
'index' => 'crud.'.$this->name.'.index', |
66
|
|
|
'create' => 'crud.'.$this->name.'.create', |
67
|
|
|
'store' => 'crud.'.$this->name.'.store', |
68
|
|
|
'edit' => 'crud.'.$this->name.'.edit', |
69
|
|
|
'update' => 'crud.'.$this->name.'.update', |
70
|
|
|
'show' => 'crud.'.$this->name.'.show', |
71
|
|
|
'destroy' => 'crud.'.$this->name.'.destroy', |
72
|
|
|
], |
73
|
|
|
], $this->options); |
74
|
|
|
|
75
|
|
|
Route::resource($this->name, $this->controller, $options_with_default_route_names); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Call other methods in this class, that register extra routes. |
80
|
|
|
* |
81
|
|
|
* @param [type] $injectables [description] |
|
|
|
|
82
|
|
|
* @return [type] [description] |
|
|
|
|
83
|
|
|
*/ |
84
|
|
|
public function with($injectables) |
85
|
|
|
{ |
86
|
|
|
if (is_string($injectables)) { |
87
|
|
|
$this->extraRoutes[] = 'with'.ucwords($injectables); |
88
|
|
|
} elseif (is_array($injectables)) { |
89
|
|
|
foreach ($injectables as $injectable) { |
90
|
|
|
$this->extraRoutes[] = 'with'.ucwords($injectable); |
91
|
|
|
} |
92
|
|
|
} else { |
93
|
|
|
$reflection = new \ReflectionFunction($injectables); |
94
|
|
|
|
95
|
|
|
if ($reflection->isClosure()) { |
96
|
|
|
$this->extraRoutes[] = $injectables; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $this->registerExtraRoutes(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* TODO |
105
|
|
|
* Give developers the ability to unregister a route. |
106
|
|
|
*/ |
107
|
|
|
// public function without($injectables) {} |
|
|
|
|
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Register the routes that were passed using the "with" syntax. |
111
|
|
|
*/ |
112
|
|
|
private function registerExtraRoutes() |
113
|
|
|
{ |
114
|
|
|
foreach ($this->extraRoutes as $route) { |
115
|
|
|
if (is_string($route)) { |
116
|
|
|
$this->{$route}(); |
117
|
|
|
} else { |
118
|
|
|
$route(); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function __call($method, $parameters = null) |
124
|
|
|
{ |
125
|
|
|
if (method_exists($this, $method)) { |
126
|
|
|
$this->{$method}($parameters); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.