1 | <?php |
||
2 | |||
3 | namespace mvc\core; |
||
4 | use mvc\core\http\Request; |
||
5 | use mvc\core\http\Response; |
||
6 | |||
7 | class Router |
||
8 | { |
||
9 | public function getParams(): array |
||
10 | { |
||
11 | $uri = Request::getUri(); |
||
12 | return explode('/', $uri == '/' ? "Site/index" : substr($uri, 1)); |
||
13 | } |
||
14 | |||
15 | public function resolve($override_params = []) |
||
16 | { |
||
17 | $params = empty($override_params) ? $this->getParams() : $override_params; |
||
18 | $controllerName = ucfirst(array_shift($params)) . 'Controller'; |
||
19 | |||
20 | if (!file_exists(ROOT_DIR . "/app/controllers/$controllerName.php")) { |
||
21 | array_unshift($params, $controllerName); |
||
22 | $controllerName = 'SiteController'; |
||
23 | } |
||
24 | |||
25 | require_once ROOT_DIR . '/app/controllers/' . $controllerName . '.php'; |
||
26 | |||
27 | $controller = new $controllerName(); |
||
28 | var_dump($params); |
||
0 ignored issues
–
show
Security
Debugging Code
introduced
by
![]() |
|||
29 | $methodName = strtolower(array_shift($params)); |
||
30 | |||
31 | if (!method_exists($controller, $methodName) && $controllerName != 'SiteController') { |
||
32 | $methodName = 'index'; |
||
33 | } |
||
34 | |||
35 | if (!method_exists($controller, $methodName)) { |
||
36 | Response::status(404); |
||
37 | return $this->resolve(['Error', 'notFound']); |
||
38 | } |
||
39 | |||
40 | return $controller->$methodName($params); |
||
41 | } |
||
42 | } |