Commit d3d3ffc1 authored by Taylor Otwell's avatar Taylor Otwell

refactoring.

parent a36e2773
...@@ -76,7 +76,7 @@ class Router { ...@@ -76,7 +76,7 @@ class Router {
{ {
// PHP 5.3.2 has a bug that causes closures cast as arrays // PHP 5.3.2 has a bug that causes closures cast as arrays
// to yield an empty array. We will work around this by // to yield an empty array. We will work around this by
// manually adding the Closure instance to a new array. // manually adding the Closure instance to an array.
if ($action instanceof Closure) $action = array($action); if ($action instanceof Closure) $action = array($action);
static::$routes[$uri] = (array) $action; static::$routes[$uri] = (array) $action;
...@@ -87,8 +87,7 @@ class Router { ...@@ -87,8 +87,7 @@ class Router {
} }
/** /**
* Find a route by name. * Find a route by the route's assigned name.
*
* *
* @param string $name * @param string $name
* @return array * @return array
...@@ -142,42 +141,26 @@ class Router { ...@@ -142,42 +141,26 @@ class Router {
// If we can't find a literal match, we'll iterate through all of // If we can't find a literal match, we'll iterate through all of
// the registered routes attempting to find a matching route that // the registered routes attempting to find a matching route that
// uses wildcards or regular expressions. // uses wildcards or regular expressions.
if ( ! is_null($route = static::search($destination)))
{
return $route;
}
// If there are no literal matches and no routes that match the
// request, we'll use convention to search for a controller to
// handle the request. If no controller can be found, the 404
// error response will be returned by the application.
$segments = array_diff(explode('/', trim($uri, '/')), array(''));
return static::controller(DEFAULT_BUNDLE, $method, $destination, $segments);
}
/**
* Attempt to match a destination to one of the registered routes.
*
* @param string $destination
* @return Route
*/
protected static function search($destination)
{
foreach (static::$routes as $route => $action) foreach (static::$routes as $route => $action)
{ {
// Since routes that don't use wildcards or regular expressions
// should have been caught by the literal route check, we will
// only check routes that have a parentheses, indicating that
// there are wildcards or regular expressions.
if (strpos($route, '(') !== false) if (strpos($route, '(') !== false)
{ {
if (preg_match('#^'.static::wildcards($route).'$#', $destination, $parameters)) $pattern = '#^'.static::wildcards($route).'$#';
if (preg_match($pattern, $destination, $parameters))
{ {
return new Route($route, $action, array_slice($parameters, 1)); return new Route($route, $action, array_slice($parameters, 1));
} }
} }
} }
// If there are no literal matches and no routes that match the
// request, we'll use convention to search for a controller to
// handle the request. If no controller can be found, the 404
// error response will be returned.
$segments = array_diff(explode('/', trim($uri, '/')), array(''));
return static::controller(DEFAULT_BUNDLE, $method, $destination, $segments);
} }
/** /**
......
...@@ -250,7 +250,9 @@ class Str { ...@@ -250,7 +250,9 @@ class Str {
*/ */
public static function classify($value) public static function classify($value)
{ {
return str_replace(' ', '_', static::title(str_replace(array('_', '.'), ' ', $value))); $search = array('_', '-', '.');
return str_replace(' ', '_', static::title(str_replace($search, ' ', $value)));
} }
/** /**
......
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