Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * A simple php router
- * Suppose that your controller is in ./controllers/
- * use :
- * Router::getInstance();
- * Router::get('Your_url','Controller@method_to_call');
- *
- * Example :
- * For the url www.yourdomaine.com/blog
- *
- * Router::getInstance();
- * Router::get('blog','BlogController@post');
- **/
- class Router
- {
- public static $instance;
- public function __construct() {
- self::$instance = $this;
- }
- static public function get($path, $arg)
- {
- $url = trim($_SERVER['REQUEST_URI'], '/');
- if($url == $path) {
- $data = explode("@",$arg);
- $file = 'controllers/'.$data[0].'.php';
- $method = $data[1];
- $controller = $data[0];
- if(file_exists($file)) {
- include($file);
- $Class = new $controller();
- call_user_func([$Class,$method]);
- die;
- }
- }
- }
- static public function getInstance() {
- if (self::$instance === null) {
- self::$instance = new self();
- }
- return self::$instance;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement