1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Erykai\Routes; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Route define route |
7
|
|
|
*/ |
8
|
|
|
class Route extends Resource |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* construct |
12
|
|
|
*/ |
13
|
|
|
public function __construct() |
14
|
|
|
{ |
15
|
|
|
$this->setMethod(); |
16
|
|
|
$this->setResponse(200, "success", "return correct route"); |
17
|
|
|
} |
18
|
|
|
/** |
19
|
|
|
* @param string $namespace |
20
|
|
|
*/ |
21
|
|
|
public function namespace(string $namespace): void |
22
|
|
|
{ |
23
|
|
|
$this->setNamespace($namespace); |
24
|
|
|
} |
25
|
|
|
/** |
26
|
|
|
* @param string $callback |
27
|
|
|
* @param string $controller |
28
|
|
|
* @param bool $middleware |
29
|
|
|
* @param string $type |
30
|
|
|
*/ |
31
|
|
|
public function get(string $callback, string $controller, bool $middleware = false, string $type = "object"): void |
32
|
|
|
{ |
33
|
|
|
$this->callback($callback,$controller,"GET",$middleware, $type); |
34
|
|
|
} |
35
|
|
|
/** |
36
|
|
|
* @param string $callback |
37
|
|
|
* @param string $controller |
38
|
|
|
* @param bool $middleware |
39
|
|
|
* @param string $type |
40
|
|
|
*/ |
41
|
|
|
public function post(string $callback, string $controller, bool $middleware = false, string $type = "object"): void |
42
|
|
|
{ |
43
|
|
|
$this->callback($callback,$controller,"POST",$middleware,$type); |
44
|
|
|
} |
45
|
|
|
/** |
46
|
|
|
* @param string $callback |
47
|
|
|
* @param string $controller |
48
|
|
|
* @param bool $middleware |
49
|
|
|
* @param string $type |
50
|
|
|
*/ |
51
|
|
|
public function put(string $callback, string $controller, bool $middleware = false, string $type = "object"): void |
52
|
|
|
{ |
53
|
|
|
$this->callback($callback,$controller,"PUT",$middleware,$type); |
54
|
|
|
} |
55
|
|
|
/** |
56
|
|
|
* @param string $callback |
57
|
|
|
* @param string $controller |
58
|
|
|
* @param bool $middleware |
59
|
|
|
* @param string $type |
60
|
|
|
*/ |
61
|
|
|
public function delete(string $callback, string $controller, bool $middleware = false, string $type = "object"): void |
62
|
|
|
{ |
63
|
|
|
$this->callback($callback,$controller,"DELETE",$middleware,$type); |
64
|
|
|
} |
65
|
|
|
/** |
66
|
|
|
* @param string $callback |
67
|
|
|
* @param string $controller |
68
|
|
|
* @param array|false[] $middleware |
69
|
|
|
* @param string $type |
70
|
|
|
*/ |
71
|
|
|
public function default(string $callback, string $controller, array $middleware = [false,false,false,false], $type = "object"): void |
72
|
|
|
{ |
73
|
|
|
$this->get($callback,"$controller@read",$middleware[0], $type); |
74
|
|
|
$this->get("$callback/{id}","$controller@read",$middleware[0],$type); |
75
|
|
|
$this->post($callback,"$controller@store",$middleware[1],$type); |
76
|
|
|
$this->put("$callback/{id}","$controller@edit",$middleware[2],$type); |
77
|
|
|
$this->delete("$callback/{id}","$controller@destroy",$middleware[3],$type); |
78
|
|
|
} |
79
|
|
|
/** |
80
|
|
|
* @return object |
81
|
|
|
*/ |
82
|
|
|
public function response(): object |
83
|
|
|
{ |
84
|
|
|
return $this->getResponse(); |
85
|
|
|
} |
86
|
|
|
/** |
87
|
|
|
* response |
88
|
|
|
*/ |
89
|
|
|
public function exec(): void |
90
|
|
|
{ |
91
|
|
|
if(!$this->controller()){ |
92
|
|
|
header("Refresh:0"); |
93
|
|
|
} |
94
|
|
|
if ($this->isNotFound()) { |
95
|
|
|
$this->setResponse( |
96
|
|
|
404, |
97
|
|
|
"error", |
98
|
|
|
"this path does not exist or has been removed" |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |