I use URL routing on my website and this is the code I use:
/* TURN FULL URL INTO --> /example/example2/example3 */
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;
}
$base_url = getCurrentUri();
$routes = array();
$routes = explode('/', $base_url);
foreach($routes as $route)
{
if(trim($route) != ''){
array_push($routes, $route);
}
}
For some reason it outputs the following when using var_dump()
on $routes[0]
, $routes[1]
, $routes[2]
and $routes[3]
. This is while being on the following website: http://localhost/tutorials/
string '' (length=0)
string 'tutorials' (length=9)
string 'tutorials' (length=9)
null
How can I solve this routing problem?