Commit 31cf44c3 authored by Taylor Otwell's avatar Taylor Otwell

working on routing.

parent 4330124d
...@@ -56,32 +56,53 @@ class Route { ...@@ -56,32 +56,53 @@ class Route {
// Extract each URI from the route key. Since the route key has the request // Extract each URI from the route key. Since the route key has the request
// method, we will extract that from the string. If the URI points to the // method, we will extract that from the string. If the URI points to the
// root of the application, a single forward slash will be returned. // root of the application, a single forward slash is returned.
$uris = array_get($action, 'handles', array($key)); $uris = array_get($action, 'handles', array($key));
$this->uris = array_map(array($this, 'extract'), $uris); $this->uris = array_map(array($this, 'destination'), $uris);
// Determine the bundle in which the route was registered. We will know // Determine the bundle in which the route was registered. We will know
// the bundle by feeding the URI into the bundle::handles method, which // the bundle by using the bundle::handles method, which will return
// will return the bundle assigned to that URI. // the bundle assigned to that URI.
$this->bundle = Bundle::handles($this->uris[0]); $this->bundle = Bundle::handles($this->uris[0]);
$this->parameters = array_map('urldecode', $parameters); $defaults = array_get($action, 'defaults', array());
$this->parameters = array_merge($parameters, $defaults);
// Once we have set the parameters and URIs, we'll transpose the route
// parameters onto the URIs so that the routes response naturally to
// the handles without the wildcards messing them up.
foreach ($this->uris as &$uri)
{
$uri = $this->transpose($uri, $this->parameters);
}
} }
/** /**
* Retrieve the URI from a given route destination. * Substitute the parameters in a given URI.
* *
* If the request is to the application root, a slash is returned. * @param string $uri
* * @param array $parameters
* @param string $segment
* @return string * @return string
*/ */
protected static function extract($segment) public static function transpose($uri, $parameters)
{ {
$uri = substr($segment, strpos($segment, ' ') + 1); // Spin through each route parameter and replace the route wildcard segment
// with the corresponding parameter passed to the method. Afterwards, we'll
// replace all of the remaining optional URI segments.
foreach ((array) $parameters as $parameter)
{
if ( ! is_null($parameter))
{
$uri = preg_replace('/\(.+?\)/', $parameter, $uri, 1);
}
}
return ($uri !== '/') ? trim($uri, '/') : $uri; // If there are any remaining optional place-holders, we'll just replace
// them with empty strings since not every optional parameter has to be
// in the array of parameters that were passed.
return str_replace(array_keys(Router::$optional), '', $uri);
} }
/** /**
...@@ -92,9 +113,8 @@ class Route { ...@@ -92,9 +113,8 @@ class Route {
public function call() public function call()
{ {
// The route is responsible for running the global filters, and any // The route is responsible for running the global filters, and any
// filters defined on the route itself. Since all incoming requests // filters defined on the route itself, since all incoming requests
// come through a route (either defined or ad-hoc), it makes sense // come through a route (either defined or ad-hoc).
// to let the route handle the global filters.
$response = Filter::run($this->filters('before'), array(), true); $response = Filter::run($this->filters('before'), array(), true);
if (is_null($response)) if (is_null($response))
...@@ -102,6 +122,9 @@ class Route { ...@@ -102,6 +122,9 @@ class Route {
$response = $this->response(); $response = $this->response();
} }
// We always return a Response instance from the route calls, so
// we'll use the prepare method on the Response class to make
// sure we have a valid Response isntance.
$response = Response::prepare($response); $response = Response::prepare($response);
Filter::run($this->filters('after'), array($response)); Filter::run($this->filters('after'), array($response));
...@@ -218,6 +241,39 @@ class Route { ...@@ -218,6 +241,39 @@ class Route {
})); }));
} }
/**
* Register a route with the router.
*
* <code>
* // Register a route with the router
* Router::register('GET /', function() {return 'Home!';});
*
* // Register a route that handles multiple URIs with the router
* Router::register(array('GET /', 'GET /home'), function() {return 'Home!';});
* </code>
*
* @param string|array $route
* @param mixed $action
* @param bool $https
* @return void
*/
public static function to($route, $action)
{
Router::register($route, $action);
}
/**
* Register a HTTPS route with the router.
*
* @param string|array $route
* @param mixed $action
* @return void
*/
public static function secure($route, $action)
{
Router::secure($route, $action);
}
/** /**
* Extract the URI string from a route destination. * Extract the URI string from a route destination.
* *
......
...@@ -204,7 +204,7 @@ class URL { ...@@ -204,7 +204,7 @@ class URL {
// Since assets are not served by Laravel, we do not need to come through // Since assets are not served by Laravel, we do not need to come through
// the front controller. So, we'll remove the application index specified // the front controller. So, we'll remove the application index specified
// in the application configuration from the generated URL. // in the application configuration from the URL.
if (($index = Config::get('application.index')) !== '') if (($index = Config::get('application.index')) !== '')
{ {
$url = str_replace($index.'/', '', $url); $url = str_replace($index.'/', '', $url);
...@@ -246,30 +246,4 @@ class URL { ...@@ -246,30 +246,4 @@ class URL {
return static::to(Route::transpose($uri, $parameters), $https); return static::to(Route::transpose($uri, $parameters), $https);
} }
/**
* Substitute the parameters in a given URI.
*
* @param string $uri
* @param array $parameters
* @return string
*/
public static function transpose($uri, $parameters)
{
// Spin through each route parameter and replace the route wildcard segment
// with the corresponding parameter passed to the method. Afterwards, we'll
// replace all of the remaining optional URI segments.
foreach ((array) $parameters as $parameter)
{
if ( ! is_null($parameter))
{
$uri = preg_replace('/\(.+?\)/', $parameter, $uri, 1);
}
}
// If there are any remaining optional place-holders, we'll just replace
// them with empty strings since not every optional parameter has to be
// in the array of parameters that were passed.
return str_replace(array_keys(Router::$optional), '', $uri);
}
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment