loader.php 1.49 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
<?php namespace System\Route;

class Loader {

	/**
	 * Load the route file based on the first segment of the URI.
	 *
	 * @param  string  $uri
	 * @return void
	 */
	public static function load($uri)
	{
		// --------------------------------------------------------------
14
		// If a single route file is being used, return it.
15 16 17 18 19 20
		// --------------------------------------------------------------
		if ( ! is_dir(APP_PATH.'routes'))
		{
			return require APP_PATH.'routes'.EXT;			
		}

21 22 23 24 25
		if ( ! file_exists(APP_PATH.'routes/home'.EXT))
		{
			throw new \Exception("A [home] route file is required when using a route directory.");					
		}

26 27
		// --------------------------------------------------------------
		// If the request is to the root, load the "home" routes file.
28 29 30
		//
		// Otherwise, load the route file matching the first segment of
		// the URI as well as the "home" routes file.
31 32 33 34 35 36 37 38 39
		// --------------------------------------------------------------
		if ($uri == '/')
		{
			return require APP_PATH.'routes/home'.EXT;
		}
		else
		{
			$segments = explode('/', trim($uri, '/'));

40 41 42
			// --------------------------------------------------------------
			// If the file doesn't exist, we'll just return the "home" file.
			// --------------------------------------------------------------
43 44
			if ( ! file_exists(APP_PATH.'routes/'.$segments[0].EXT))
			{
45
				return require APP_PATH.'routes/home'.EXT;
46 47 48 49 50 51 52
			}

			return array_merge(require APP_PATH.'routes/'.$segments[0].EXT, require APP_PATH.'routes/home'.EXT);
		}
	}

}