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