Router.php 938 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. Lib::loadClass('HttpException');
  3. class Router {
  4. public static function route($route) {
  5. if (empty($route)) return;
  6. try {
  7. $route = self::getRoute($route);
  8. $route->route();
  9. } catch (HttpException $e) {
  10. Http::sendHeaderByCode($e->getCode());
  11. die($e->getMessage());
  12. } catch (Exception $e) {
  13. die($e->getMessage());
  14. }
  15. exit;
  16. }
  17. public static function handleAuth($route) {
  18. if (empty($route)) return;
  19. try {
  20. $route = self::getRoute($route);
  21. $route->handleAuth();
  22. } catch (HttpException $e) {
  23. Http::sendHeaderByCode($e->getCode());
  24. die($e->getMessage());
  25. } catch (Exception $e) {
  26. die($e->getMessage());
  27. }
  28. }
  29. public static function getRoute($route) {
  30. if (empty($route)) return;
  31. $routeClassName = "Route_{$route}";
  32. if (Lib::tryLoadClass($routeClassName)) {
  33. $route = new $routeClassName();
  34. return $route;
  35. } else {
  36. throw new HttpException("Not found", 404);
  37. }
  38. }
  39. }