Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. <?php
  2.  
  3. namespace application\core;
  4.  
  5. class Router {
  6.  
  7. protected $routes = [];
  8. protected $params = [];
  9.  
  10. public function __construct() {
  11. $arr = require 'application/config/routes.php';
  12. foreach($arr as $key => $val) {
  13. $this->add($key, $val);
  14. }
  15. }
  16.  
  17. public function add($route, $params) {
  18. $route = '#^'.$route.'$#';
  19. $this->routes[$route] = $params;
  20. }
  21.  
  22. public function match() {
  23. $url = trim($_SERVER['REQUEST_URI'], '/');
  24. foreach($this->routes as $route => $params) {
  25. if(preg_match($route, $url, $matches)) {
  26. $this->params = $params;
  27. return true;
  28. }
  29. }
  30. return false;
  31. }
  32.  
  33. public function run() {
  34.  
  35. if($this->match()) {
  36. $controller = 'application\controllers\\'.ucfirst($this->params['controller']).'Controller.php';
  37. if(class_exists($controller)) {
  38. echo 'Класс <i>'.$controller.'</i> найден!';
  39. } else {
  40. echo 'Класс <i>'.$controller.'</i> <b style="font-size: 20px; color: red;">НЕ</b> найден!';
  41. }
  42. } else {
  43. echo 'Маршрут не найден!';
  44. }
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement