Skip to content

Commit 69b3adf

Browse files
committed
Fix(Route): Added separate functions for each type of HTTP request
1 parent 62d4c1d commit 69b3adf

File tree

2 files changed

+34
-13
lines changed

2 files changed

+34
-13
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/vendor/
22
/app/
33
/routes/
4+
/public/
45
.htaccess
56
index.php

src/LionRoute/Route.php

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,37 +69,57 @@ public static function middleware(array $middleware, \Closure $closure): void {
6969
}
7070

7171
public static function get(string $url, \Closure|array $controller_function, array $filters = []): void {
72-
self::executeMethod('get', $url, $controller_function, $filters);
72+
if (count($filters) > 0) {
73+
self::$router->get(
74+
$url,
75+
$controller_function,
76+
isset($filters[1]) ? ['before' => $filters[0], 'after' => $filters[1]] : ['before' => $filters[0]]
77+
);
78+
} else {
79+
self::$router->get($url, $controller_function);
80+
}
7381
}
7482

7583
public static function post(string $url, \Closure|array $controller_function, array $filters = []): void {
76-
self::executeMethod('post', $url, $controller_function, $filters);
84+
if (count($filters) > 0) {
85+
self::$router->post(
86+
$url,
87+
$controller_function,
88+
isset($filters[1]) ? ['before' => $filters[0], 'after' => $filters[1]] : ['before' => $filters[0]]
89+
);
90+
} else {
91+
self::$router->post($url, $controller_function);
92+
}
7793
}
7894

7995
public static function put(string $url, \Closure|array $controller_function, array $filters = []): void {
80-
self::executeMethod('put', $url, $controller_function, $filters);
96+
if (count($filters) > 0) {
97+
self::$router->put(
98+
$url,
99+
$controller_function,
100+
isset($filters[1]) ? ['before' => $filters[0], 'after' => $filters[1]] : ['before' => $filters[0]]
101+
);
102+
} else {
103+
self::$router->put($url, $controller_function);
104+
}
81105
}
82106

83107
public static function delete(string $url, \Closure|array $controller_function, array $filters = []): void {
84-
self::executeMethod('delete', $url, $controller_function, $filters);
85-
}
86-
87-
public static function any(string $url, \Closure|array $controller_function, array $filters = []): void {
88-
self::executeMethod('any', $url, $controller_function, $filters);
89-
}
90-
91-
private static function executeMethod(string $methodType, string $url, \Closure|array $controller_function, array $filters = []): void {
92108
if (count($filters) > 0) {
93-
self::$router->$methodType(
109+
self::$router->delete(
94110
$url,
95111
$controller_function,
96112
isset($filters[1]) ? ['before' => $filters[0], 'after' => $filters[1]] : ['before' => $filters[0]]
97113
);
98114
} else {
99-
self::$router->$methodType($url, $controller_function);
115+
self::$router->delete($url, $controller_function);
100116
}
101117
}
102118

119+
public static function any(string $url, \Closure|array $controller_function, array $filters = []): void {
120+
self::executeMethod('any', $url, $controller_function, $filters);
121+
}
122+
103123
private static function processInput(int $index): string {
104124
return implode('/', array_slice(explode('/', $_SERVER['REQUEST_URI']), $index));
105125
}

0 commit comments

Comments
 (0)