moved route loading into separate function.
This commit is contained in:
parent
9cee469371
commit
fd42e0ca40
|
@ -31,39 +31,11 @@ public static function route($method, $uri)
|
||||||
$uri = ($uri != '/') ? '/'.$uri : $uri;
|
$uri = ($uri != '/') ? '/'.$uri : $uri;
|
||||||
|
|
||||||
// --------------------------------------------------------------
|
// --------------------------------------------------------------
|
||||||
// If a route directory is being used, load the route file
|
// Load the application routes.
|
||||||
// corresponding to the first segment of the URI.
|
|
||||||
// --------------------------------------------------------------
|
// --------------------------------------------------------------
|
||||||
if (is_dir(APP_PATH.'routes'))
|
if (is_null(static::$routes))
|
||||||
{
|
{
|
||||||
if ($uri == '/')
|
static::$routes = static::routes($uri);
|
||||||
{
|
|
||||||
if ( ! file_exists(APP_PATH.'routes/home'.EXT))
|
|
||||||
{
|
|
||||||
throw new \Exception("A [home] route file is required when using a route directory.");
|
|
||||||
}
|
|
||||||
|
|
||||||
static::$routes = require APP_PATH.'routes/home'.EXT;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
$segments = explode('/', trim($uri, '/'));
|
|
||||||
|
|
||||||
if ( ! file_exists(APP_PATH.'routes/'.$segments[0].EXT))
|
|
||||||
{
|
|
||||||
throw new \Exception("No route file defined for routes beginning with [".$segments[0]."]");
|
|
||||||
}
|
|
||||||
|
|
||||||
static::$routes = require APP_PATH.'routes/'.$segments[0].EXT;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// --------------------------------------------------------------
|
|
||||||
// If no route directory is being used, we can simply load the
|
|
||||||
// routes file from the application directory.
|
|
||||||
// --------------------------------------------------------------
|
|
||||||
else
|
|
||||||
{
|
|
||||||
static::$routes = require APP_PATH.'routes'.EXT;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// --------------------------------------------------------------
|
// --------------------------------------------------------------
|
||||||
|
@ -150,4 +122,49 @@ private static function parameters($uri_segments, $route_segments)
|
||||||
return $parameters;
|
return $parameters;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Load the routes based on the request URI.
|
||||||
|
*
|
||||||
|
* @param string $uri
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
private static function routes($uri)
|
||||||
|
{
|
||||||
|
// --------------------------------------------------------------
|
||||||
|
// If a route directory is being used, load the route file
|
||||||
|
// corresponding to the first segment of the URI.
|
||||||
|
// --------------------------------------------------------------
|
||||||
|
if (is_dir(APP_PATH.'routes'))
|
||||||
|
{
|
||||||
|
if ($uri == '/')
|
||||||
|
{
|
||||||
|
if ( ! file_exists(APP_PATH.'routes/home'.EXT))
|
||||||
|
{
|
||||||
|
throw new \Exception("A [home] route file is required when using a route directory.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return require APP_PATH.'routes/home'.EXT;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$segments = explode('/', trim($uri, '/'));
|
||||||
|
|
||||||
|
if ( ! file_exists(APP_PATH.'routes/'.$segments[0].EXT))
|
||||||
|
{
|
||||||
|
throw new \Exception("No route file defined for routes beginning with [".$segments[0]."]");
|
||||||
|
}
|
||||||
|
|
||||||
|
return require APP_PATH.'routes/'.$segments[0].EXT;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// --------------------------------------------------------------
|
||||||
|
// If no route directory is being used, we can simply load the
|
||||||
|
// routes file from the application directory.
|
||||||
|
// --------------------------------------------------------------
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return require APP_PATH.'routes'.EXT;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue