-
-
Save schnabear/a3a9f2f4443aeac97aee to your computer and use it in GitHub Desktop.
Rails Like PHP URL Router
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
RewriteEngine On | |
# RewriteBase / | |
RewriteCond %{REQUEST_FILENAME} !-f | |
RewriteCond %{REQUEST_FILENAME} !-d | |
RewriteRule ^(.*)$ index.php [QSA,L] |
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 | |
require_once 'route.php'; | |
Route::map("/user/:id/:name", | |
function($id, $name) { | |
echo $id . ' - ' . $name; | |
} | |
); |
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 | |
// Reference: http://blog.sosedoff.com/2009/09/20/rails-like-php-url-router/ | |
class Route | |
{ | |
public static function requestUri() | |
{ | |
$uri = ''; | |
if (empty($_SERVER['PATH_INFO']) === false) { | |
$uri = $_SERVER['PATH_INFO']; | |
} else { | |
if (isset($_SERVER['REQUEST_URI']) === true) { | |
$scheme = empty($_SERVER['HTTPS']) === true || $_SERVER['HTTPS'] === 'off' ? 'http' : 'https'; | |
$uri = parse_url($scheme . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], PHP_URL_PATH); | |
} elseif (isset($_SERVER['PHP_SELF']) === true) { | |
$uri = $_SERVER['PHP_SELF']; | |
} else { | |
return ""; | |
} | |
$request_uri = isset($_SERVER['REQUEST_URI']) === true ? $_SERVER['REQUEST_URI'] : $_SERVER['PHP_SELF']; | |
$script_name = $_SERVER['SCRIPT_NAME']; | |
$base_uri = strpos($request_uri, $script_name) === 0 ? $script_name : str_replace('\\', '/', dirname($script_name)); | |
$base_uri = rtrim($base_uri, '/'); | |
if ($base_uri !== '' && strpos($uri, $base_uri) === 0) { | |
$uri = substr($uri, strlen($base_uri)); | |
} | |
return '/' . ltrim($uri, '/'); | |
} | |
} | |
public static function map($pattern_uri, $callback = null) | |
{ | |
$request_uri = self::requestUri(); | |
// Get names: :id, :name | |
preg_match_all('/:([\w]+)/', $pattern_uri, $names, PREG_PATTERN_ORDER); | |
$names = $names[0]; | |
// Form: /user/:id/:name | |
// To : /user/([A-Za-z0-9_]+)/([A-Za-z0-9_]+)/? | |
$pattern_uri_regex = preg_replace_callback('/:[\w]+/', array(__CLASS__, 'patternUriRegex'), $pattern_uri); | |
$pattern_uri_regex .= '/?'; | |
// Match pattern uri regex on request uri, fill params | |
$params = array(); | |
if (preg_match('@^' . $pattern_uri_regex . '$@', $request_uri, $values)) { | |
array_shift($values); | |
foreach($names as $index => $value) { | |
$params[substr($value, 1)] = urldecode($values[$index]); | |
} | |
if (is_callable($callback) === true) { | |
call_user_func_array($callback, array_values($params)); | |
} | |
} | |
return $params; | |
} | |
public static function patternUriRegex($matches) | |
{ | |
return '([a-zA-Z0-9_\+\-%]+)'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment