Advertisement
Guest User

Untitled

a guest
Nov 26th, 2015
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. <?php
  2. /**
  3. * A simple php router
  4. * Suppose that your controller is in ./controllers/
  5. * use :
  6. * Router::getInstance();
  7. * Router::get('Your_url','Controller@method_to_call');
  8. *
  9. * Example :
  10. * For the url www.yourdomaine.com/blog
  11. *
  12. * Router::getInstance();
  13. * Router::get('blog','BlogController@post');
  14. **/
  15. class Router
  16. {
  17. public static $instance;
  18.  
  19. public function __construct() {
  20. self::$instance = $this;
  21. }
  22.  
  23. static public function get($path, $arg)
  24. {
  25. $url = trim($_SERVER['REQUEST_URI'], '/');
  26.  
  27. if($url == $path) {
  28. $data = explode("@",$arg);
  29. $file = 'controllers/'.$data[0].'.php';
  30. $method = $data[1];
  31. $controller = $data[0];
  32.  
  33. if(file_exists($file)) {
  34. include($file);
  35. $Class = new $controller();
  36. call_user_func([$Class,$method]);
  37. die;
  38. }
  39. }
  40. }
  41.  
  42. static public function getInstance() {
  43. if (self::$instance === null) {
  44. self::$instance = new self();
  45. }
  46. return self::$instance;
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement