|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: afshin |
|
5
|
|
|
* Date: 11/25/17 |
|
6
|
|
|
* Time: 11:00 AM |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Core; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
use Psr\Container\ContainerInterface; |
|
14
|
|
|
|
|
15
|
|
|
class Route |
|
16
|
|
|
{ |
|
17
|
|
|
|
|
18
|
|
|
protected $app; |
|
19
|
|
|
|
|
20
|
|
|
function __construct($app) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->app = $app; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Get Slim Container |
|
27
|
|
|
* |
|
28
|
|
|
* @return ContainerInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
protected function getContainer() |
|
31
|
|
|
{ |
|
32
|
|
|
return $this->app->container; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
public function resource($url, $controller, $args = []) |
|
38
|
|
|
{ |
|
39
|
|
|
$url = rtrim($url,'/'); |
|
40
|
|
|
$routeNames = implode('.',explode('/',trim($url,'/'))); |
|
41
|
|
|
|
|
42
|
|
|
$this->app->group($url, function () use ($controller,$routeNames, $args) { |
|
43
|
|
|
$this->get('', $controller . ':index')->add(self::middleware('index', $args))->setName($routeNames.'.index'); |
|
44
|
|
|
$this->get('/', $controller . ':index')->add(self::middleware('index', $args))->setName($routeNames.'.index'); |
|
45
|
|
|
$this->get('/index', $controller . ':index')->add(self::middleware('index', $args))->setName($routeNames.'.index'); |
|
46
|
|
|
|
|
47
|
|
|
$this->get('/create', $controller . ':create')->add(self::middleware('create', $args))->setName($routeNames.'.create'); |
|
48
|
|
|
$this->get('/{id:[0-9]+}', $controller . ':show')->add(self::middleware('show', $args))->setName($routeNames.'.show'); |
|
49
|
|
|
$this->get('/{id:[0-9]+}/edit', $controller . ':edit')->add(self::middleware('edit', $args))->setName($routeNames.'.edit'); |
|
50
|
|
|
$this->post('', $controller . ':store')->add(self::middleware('store', $args))->setName($routeNames.'.store'); |
|
51
|
|
|
$this->put('/{id:[0-9]+}', $controller . ':update')->add(self::middleware('update', $args))->setName($routeNames.'.update'); |
|
52
|
|
|
$this->patch('/{id:[0-9]+}', $controller . ':update')->add(self::middleware('index', $args))->setName($routeNames.'.update'); |
|
53
|
|
|
$this->delete('/{id:[0-9]+}', $controller . ':destroy')->add(self::middleware('destroy', $args))->setName($routeNames.'.destroy'); |
|
54
|
|
|
|
|
55
|
|
|
})->add(self::middleware('group', $args)); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param string $middlewareType |
|
60
|
|
|
* @param $args |
|
61
|
|
|
* @return \Closure |
|
62
|
|
|
*/ |
|
63
|
|
|
public static function middleware($middleWare = "group", $args) |
|
64
|
|
|
{ |
|
65
|
|
|
$routeResourceMiddleWare_default = function ($request, $response, $next) { |
|
66
|
|
|
return $next($request, $response); |
|
67
|
|
|
}; |
|
68
|
|
|
|
|
69
|
|
|
return $middleware = $args['middleware'][$middleWare] ?? $routeResourceMiddleWare_default; |
|
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
/******************************************************************************** |
|
75
|
|
|
* Router proxy methods |
|
76
|
|
|
*******************************************************************************/ |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Add GET route |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $pattern The route URI pattern |
|
82
|
|
|
* @param callable|string $callable The route callback routine |
|
83
|
|
|
* |
|
84
|
|
|
* @return \Slim\Interfaces\RouteInterface |
|
85
|
|
|
*/ |
|
86
|
|
|
public function get($pattern, $callable) |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->app->map(['GET'], $pattern, $callable); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Add POST route |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $pattern The route URI pattern |
|
95
|
|
|
* @param callable|string $callable The route callback routine |
|
96
|
|
|
* |
|
97
|
|
|
* @return \Slim\Interfaces\RouteInterface |
|
98
|
|
|
*/ |
|
99
|
|
|
public function post($pattern, $callable) |
|
100
|
|
|
{ |
|
101
|
|
|
return $this->app->map(['POST'], $pattern, $callable); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Add PUT route |
|
106
|
|
|
* |
|
107
|
|
|
* @param string $pattern The route URI pattern |
|
108
|
|
|
* @param callable|string $callable The route callback routine |
|
109
|
|
|
* |
|
110
|
|
|
* @return \Slim\Interfaces\RouteInterface |
|
111
|
|
|
*/ |
|
112
|
|
|
public function put($pattern, $callable) |
|
113
|
|
|
{ |
|
114
|
|
|
return $this->app->map(['PUT'], $pattern, $callable); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Add PATCH route |
|
119
|
|
|
* |
|
120
|
|
|
* @param string $pattern The route URI pattern |
|
121
|
|
|
* @param callable|string $callable The route callback routine |
|
122
|
|
|
* |
|
123
|
|
|
* @return \Slim\Interfaces\RouteInterface |
|
124
|
|
|
*/ |
|
125
|
|
|
public function patch($pattern, $callable) |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->app->map(['PATCH'], $pattern, $callable); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Add DELETE route |
|
132
|
|
|
* |
|
133
|
|
|
* @param string $pattern The route URI pattern |
|
134
|
|
|
* @param callable|string $callable The route callback routine |
|
135
|
|
|
* |
|
136
|
|
|
* @return \Slim\Interfaces\RouteInterface |
|
137
|
|
|
*/ |
|
138
|
|
|
public function delete($pattern, $callable) |
|
139
|
|
|
{ |
|
140
|
|
|
return $this->app->map(['DELETE'], $pattern, $callable); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Add OPTIONS route |
|
145
|
|
|
* |
|
146
|
|
|
* @param string $pattern The route URI pattern |
|
147
|
|
|
* @param callable|string $callable The route callback routine |
|
148
|
|
|
* |
|
149
|
|
|
* @return \Slim\Interfaces\RouteInterface |
|
150
|
|
|
*/ |
|
151
|
|
|
public function options($pattern, $callable) |
|
152
|
|
|
{ |
|
153
|
|
|
return $this->app->map(['OPTIONS'], $pattern, $callable); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Add route for any HTTP method |
|
158
|
|
|
* |
|
159
|
|
|
* @param string $pattern The route URI pattern |
|
160
|
|
|
* @param callable|string $callable The route callback routine |
|
161
|
|
|
* |
|
162
|
|
|
* @return \Slim\Interfaces\RouteInterface |
|
163
|
|
|
*/ |
|
164
|
|
|
public function any($pattern, $callable) |
|
165
|
|
|
{ |
|
166
|
|
|
return $this->app->map(['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'], $pattern, $callable); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
|
|
170
|
|
|
} |