1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* slince routing library |
4
|
|
|
* @author Tao <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
namespace Slince\Routing; |
7
|
|
|
|
8
|
|
|
trait RouteBuilderTrait |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Creates a new route with http scheme |
12
|
|
|
* @param string $path |
13
|
|
|
* @param mixed $action |
14
|
|
|
* @return Route |
15
|
|
|
*/ |
16
|
|
|
public function http($path, $action) |
17
|
|
|
{ |
18
|
|
|
return $this->create($path, $action)->setSchemes(['http']); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Creates a new route with https scheme |
23
|
|
|
* @param string $path |
24
|
|
|
* @param mixed $action |
25
|
|
|
* @return Route |
26
|
|
|
*/ |
27
|
|
|
public function https($path, $action) |
28
|
|
|
{ |
29
|
|
|
return $this->create($path, $action)->setSchemes(['https']); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Creates a new GET route |
34
|
|
|
* @param string $path |
35
|
|
|
* @param mixed $action |
36
|
|
|
* @return Route |
37
|
|
|
*/ |
38
|
|
|
public function get($path, $action) |
39
|
|
|
{ |
40
|
|
|
return $this->create($path, $action)->setMethods([ |
41
|
|
|
Route::GET, |
42
|
|
|
Route::HEAD |
43
|
|
|
]); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Creates a new POST route |
48
|
|
|
* @param string $path |
49
|
|
|
* @param mixed $action |
50
|
|
|
* @return Route |
51
|
|
|
*/ |
52
|
|
|
public function post($path, $action) |
53
|
|
|
{ |
54
|
|
|
return $this->create($path, $action)->setMethods([ |
55
|
|
|
Route::POST |
56
|
|
|
]); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Creates a new PUT route |
61
|
|
|
* @param string $path |
62
|
|
|
* @param mixed $action |
63
|
|
|
* @return Route |
64
|
|
|
*/ |
65
|
|
|
public function put($path, $action) |
66
|
|
|
{ |
67
|
|
|
return $this->create($path, $action)->setMethods([ |
68
|
|
|
Route::PUT |
69
|
|
|
]); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Creates a new PATCH route |
74
|
|
|
* @param string $path |
75
|
|
|
* @param mixed $action |
76
|
|
|
* @return Route |
77
|
|
|
*/ |
78
|
|
|
public function patch($path, $action) |
79
|
|
|
{ |
80
|
|
|
return $this->create($path, $action)->setMethods([ |
81
|
|
|
Route::PATCH |
82
|
|
|
]); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Creates a new DELETE route |
87
|
|
|
* @param string $path |
88
|
|
|
* @param mixed $action |
89
|
|
|
* @return Route |
90
|
|
|
*/ |
91
|
|
|
public function delete($path, $action) |
92
|
|
|
{ |
93
|
|
|
return $this->create($path, $action)->setMethods([ |
94
|
|
|
Route::DELETE |
95
|
|
|
]); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Creates a new route and registries it to the collection |
100
|
|
|
* @param string $path |
101
|
|
|
* @param mixed $action |
102
|
|
|
* @return Route |
103
|
|
|
*/ |
104
|
|
|
public function create($path, $action) |
105
|
|
|
{ |
106
|
|
|
$route = new Route($path, $action); |
107
|
|
|
$this->add($route); |
108
|
|
|
return $route; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Add a route to the collection |
113
|
|
|
* @param Route $route |
114
|
|
|
* @return Route |
115
|
|
|
*/ |
116
|
|
|
abstract public function add(Route $route); |
117
|
|
|
} |