Skip to content

Instantly share code, notes, and snippets.

@nasrulhazim
Created November 10, 2016 06:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nasrulhazim/6b68d6da996336333211aa2158993eae to your computer and use it in GitHub Desktop.
Save nasrulhazim/6b68d6da996336333211aa2158993eae to your computer and use it in GitHub Desktop.
Simple Route - get route details and parameters
<?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