Created
August 16, 2012 23:31
-
-
Save rafkhan/3374483 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Route urls | |
*/ | |
class Router { | |
function __construct($query_str) { | |
$this->url_str = $_GET['route']; //Get end of url | |
$this->qtokens = explode('/', string); //get tokens split at '/' | |
$this->mobile_request = is_mobile(); //Check for app request | |
$this->controller = $this->load_controller(); //Load it | |
} | |
/* | |
* Check if this is a request from the app | |
*/ | |
private function is_mobile() { | |
//Just a call to return JSON data | |
if($this->qtokens[0] == 'api') { | |
//pop 'api' off the array | |
array_shift($this->qtokens); | |
return true; | |
} else { | |
return false; | |
} | |
} | |
/* | |
* Import controller class, and instantiate it | |
*/ | |
private function load_controller() { | |
$controller_base_name = array_shift($this->qtokens); | |
//Path to controller file | |
$controller_file_path = | |
ROOT.'src/app/controllers/' | |
. $controller_base_name.'controller.php'; | |
//Check if file exists | |
if(file_exists($controller_file_path)) { | |
//Load controller | |
require_once($controller_file_path); | |
//Controllers Class name | |
//i.e ImageController, in imagecontroller.php | |
$controller_class_name = | |
ucfirst($controller_base_name) | |
. 'Controller'; | |
//Instantiate object of $controller_class_name | |
$controller_object = new $controller_class_name(); | |
//This will be used | |
return $controller_object; | |
} else { | |
//check for homepage or 404 | |
} | |
} | |
/* | |
* Check if method exists in object, if so, run it | |
*/ | |
private function call_method() { | |
//Get method from tokens | |
$method_name = array_shift($this->qtokens); | |
//Check for default controller method | |
if($method_name == NULL) { | |
$method_name = '_default'; | |
} | |
//Check to see if method exists | |
if(method_exists($this->controller, $method_name)) { | |
//Run it | |
call_user_func(array($this->controller, '_default')); | |
} else { | |
//404 page | |
$this->e404(); | |
} | |
} | |
/* | |
* Not found... | |
*/ | |
private function e404() { | |
echo "FOUR OH FOUR!!!!!!!!one <br />"; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment