Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- define('ABS_PATH', dirname($_SERVER['SCRIPT_FILENAME']) . '/');
- require_once ABS_PATH . 'load.php';
- $router = new router ();
- $router -> get_router ();
- $router -> load ();
- class router
- {
- public $uri;
- public $route;
- public $file;
- public $allowed_routes = array('home', 'login', 'signup', 'account', 'logout', 'blog');
- public $protected_subdomains = array('media');
- function __construct ($db)
- {
- if(empty($uri))
- {
- $this->uri = uri::getInstance();
- }
- }
- function get_router ()
- {
- if($this->uri->subdomain() && !in_array($this->uri->subdomain(), $this->protected_subdomains))
- {
- $this->route = 'blog';
- }
- else if(!$this->uri->fragment(1))
- {
- $this->route = 'home';
- }
- else
- {
- $this->route = $this->uri->fragment(1);
- }
- if(in_array($this->route, $this->allowed_routes))
- {
- $this->file = $this->route . '.php';
- }
- else
- {
- $this->file = '404.php';
- }
- }
- function load ()
- {
- include $this->file;
- }
- }
- class uri
- {
- public static $fragments = array();
- public static $subdomain;
- public static $domain;
- private static $instance = null;
- public static function getInstance()
- {
- if(is_null(self::$instance))
- {
- self::$instance = new uri;
- }
- return self::$instance;
- }
- private function __construct()
- {
- self::$fragments = explode('/', $_SERVER['REQUEST_URI']);
- self::$domain = explode('.', $_SERVER['SERVER_NAME']);
- }
- public function fragment($key)
- {
- if(array_key_exists($key, self::$fragments))
- {
- return self::$fragments[$key];
- }
- return false;
- }
- public function subdomain()
- {
- return self::$subdomain = (array_key_exists(count(self::$domain) - 3, self::$domain)) ? self::$domain[count(self::$domain) - 3] : false;
- }
- private function __clone()
- {
- }
- }
Add Comment
Please, Sign In to add comment