-
-
Save mfairchild365/1185305 to your computer and use it in GitHub Desktop.
Simple router idea, uses array of routes
This file contains hidden or 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 | |
class Home_Router extends Router_Template | |
{ | |
public static function getViewRoutes() | |
{ | |
return array('/^home$/i' => 'Home_View'); | |
} | |
public static function getEditRoutes() | |
{ | |
return array(); | |
} | |
public static function getDeleteRoutes() | |
{ | |
return array(); | |
} | |
} |
This file contains hidden or 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 | |
class Router | |
{ | |
public static $cacheRoutes = true; | |
public static function route($requestURI, $options = array()) | |
{ | |
if (!empty($_SERVER['QUERY_STRING'])) { | |
$requestURI = substr($requestURI, 0, -strlen($_SERVER['QUERY_STRING']) - 1); | |
} | |
// Trim the base part of the URL | |
$requestURI = substr($requestURI, strlen(parse_url(Br_Controller::$url, PHP_URL_PATH))); | |
$routes = self::getRoutes(); | |
if (isset($options['view'], $routes[$options['view']])) { | |
$options['model'] = $routes[$options['view']]; | |
return $options; | |
} | |
if (empty($requestURI)) { | |
// Default view/homepage | |
$options['model'] = "Home_View"; | |
return $options; | |
} | |
foreach ($routes as $route_exp=>$model) { | |
if ($route_exp[0] == '/' && preg_match($route_exp, $requestURI, $matches)) { | |
$options += $matches; | |
$options['model'] = $model; | |
return $options; | |
} | |
} | |
return $options; | |
} | |
public static function getRoutes() | |
{ | |
if (!self::$cacheRoutes) { | |
return self::compileRoutes(); | |
} | |
if (file_exists(self::getCachePath())) { | |
$cache = file_get_contents(self::getCachePath()); | |
return unserialize($cache); | |
} | |
return self::cacheRoutes(); | |
} | |
public static function cacheRoutes() | |
{ | |
$routes = self::compileRoutes(); | |
file_put_contents(self::getCachePath(), serialize($routes)); | |
return $routes; | |
} | |
public static function getCachePath() | |
{ | |
return sys_get_temp_dir() . "/" . __CLASS__ . "_Cache.php"; | |
} | |
public static function compileRoutes() | |
{ | |
$routes = array(); | |
//Directory itterator | |
$directory = new DirectoryIterator(dirname(__FILE__)); | |
//Compile all the routes. | |
foreach ($directory as $file) { | |
if ($file->getType() == 'dir' && !$file->isDot()) { | |
$class = "Br_" . $file->getFileName() . "_Router"; | |
if (file_exists(dirname(dirname(__FILE__)) . "/" . str_replace('_', '/', $class). ".php") | |
&& class_exists($class)) { | |
$routes += call_user_func($class . "::getRoutes"); | |
} | |
} | |
} | |
return $routes; | |
} | |
} |
This file contains hidden or 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 | |
class Router_Template | |
{ | |
public static function getViewRoutes() | |
{ | |
return array(); | |
} | |
public static function getEditRoutes() | |
{ | |
return array(); | |
} | |
public static function getDeleteRoutes() | |
{ | |
return array(); | |
} | |
public static function getOtherRoutes() | |
{ | |
return array(); | |
} | |
public static function getRoutes() | |
{ | |
$class = get_called_class(); | |
$routes = array(); | |
$routes += call_user_func($class . "::getViewRoutes"); | |
$routes += call_user_func($class . "::getEditRoutes"); | |
$routes += call_user_func($class . "::getDeleteRoutes"); | |
$routes += call_user_func($class . "::getOtherRoutes"); | |
return $routes; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
An Idea for modular routes. I think it would be a little easier for maintenance if you kept all the routes for a specific model grouped within that model.
In this example the directory is set up like this.
src/Router.php
src/Router/Template.php
src/Home/Router.php
In order to speed up results, the Routes will be cached, unless you specify otherwise.