Created
November 10, 2016 06:41
-
-
Save nasrulhazim/6b68d6da996336333211aa2158993eae to your computer and use it in GitHub Desktop.
Simple Route - get route details and parameters
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 Route | |
{ | |
public static function getCurrentUri() | |
{ | |
$basepath = implode('/', | |
array_slice( | |
explode('/', $_SERVER['SCRIPT_NAME']), | |
0, -1) | |
) . '/'; | |
$uri = substr( | |
$_SERVER['REQUEST_URI'], | |
strlen($basepath) | |
); | |
if (strstr($uri, '?')) { | |
$uri = substr($uri, 0, strpos($uri, '?')); | |
} | |
$uri = '/' . trim($uri, '/'); | |
return $uri; | |
} | |
public static function getRoute() | |
{ | |
$base_url = self::getCurrentUri(); | |
$routes = []; | |
$_routes = explode('/', $base_url); | |
foreach ($_routes as $route) { | |
if (!empty(trim($route)) && !in_array($route, $routes)) { | |
$routes[] = $route; | |
} | |
} | |
$return = [ | |
'route' => $routes, | |
'parameters' => self::getParameters(), | |
]; | |
return $return; | |
} | |
public static function getParameters() | |
{ | |
return ($_SERVER['REQUEST_METHOD'] == 'GET') ? $_GET : $_POST; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment