Commit 8737dde6 authored by Taylor Otwell's avatar Taylor Otwell

making changes to routing.

parent 38da9f47
......@@ -401,11 +401,11 @@ class Bundle {
* Get the information for a given bundle.
*
* @param string $bundle
* @return object
* @return array
*/
public static function get($bundle)
{
return (object) array_get(static::$bundles, $bundle);
return array_get(static::$bundles, $bundle);
}
/**
......
......@@ -98,9 +98,9 @@ class Bundler extends Task {
// Once we have the bundle information from the API, we'll simply
// recursively delete the bundle and then re-download it using
// the correct provider assigned to the bundle.
File::rmdir($bundle->location);
File::rmdir($bundle['location']);
$this->download($response['bundle'], $bundle->location);
$this->download($response['bundle'], $bundle['location']);
echo "Bundle [{$name}] has been upgraded!".PHP_EOL;
}
......
......@@ -9,6 +9,13 @@ class Router {
*/
public static $routes = array();
/**
* All of the "fallback" routes that have been registered.
*
* @var array
*/
public static $fallback = array();
/**
* All of the route names that have been matched with URIs.
*
......@@ -16,6 +23,13 @@ class Router {
*/
public static $names = array();
/**
* The actions that have been reverse routed.
*
* @var array
*/
public static $uses = array();
/**
* The wildcard patterns supported by the router.
*
......@@ -43,6 +57,18 @@ class Router {
*/
public static $methods = array('GET', 'POST', 'PUT', 'DELETE');
/**
* Register a HTTPS route with the router.
*
* @param string|array $route
* @param mixed $action
* @return void
*/
public static function secure($route, $action)
{
static::register($route, $action, true);
}
/**
* Register a route with the router.
*
......@@ -56,12 +82,18 @@ class Router {
*
* @param string|array $route
* @param mixed $action
* @param bool $https
* @return void
*/
public static function register($route, $action)
public static function register($route, $action, $https = false)
{
if (is_string($route)) $route = explode(', ', $route);
foreach ((array) $route as $uri)
{
// If the URI begins with a splat, we'll call the universal method, which
// will register a route for each of the request methods supported by
// the router. This is just a notational short-cut.
if (starts_with($uri, '*'))
{
static::universal(substr($uri, 2), $action);
......@@ -69,13 +101,26 @@ class Router {
continue;
}
// If the URI begins with a wildcard, we want to add this route to the
// array of "fallback" routes. Fallback routes are always processed
// last when parsing routes since they are very generic and could
// overload bundle routes that are registered.
if (str_contains($uri, ' /('))
{
$routes =& static::$fallback;
}
else
{
$routes =& static::$routes;
}
// If the action is a string, it is a pointer to a controller, so we
// need to add it to the action array as a "uses" clause, which will
// indicate to the route to call the controller when the route is
// executed by the application.
if (is_string($action))
{
static::$routes[$uri]['uses'] = $action;
$routes[$uri]['uses'] = $action;
}
// If the action is not a string, we can just simply cast it as an
// array, then we will add all of the URIs to the action array as
......@@ -85,10 +130,19 @@ class Router {
{
if ($action instanceof Closure) $action = array($action);
static::$routes[$uri] = (array) $action;
$routes[$uri] = (array) $action;
}
// If the HTTPS option is not set on the action, we will use the
// value given to the method. The "secure" method passes in the
// HTTPS value in as a parameter short-cut, just so the dev
// doesn't always have to add it to an array.
if ( ! isset($routes[$uri]['https']))
{
$routes[$uri]['https'] = $https;
}
static::$routes[$uri]['handles'] = (array) $route;
$routes[$uri]['handles'] = (array) $route;
}
}
......@@ -140,7 +194,7 @@ class Router {
// To find a named route, we will iterate through every route defined
// for the application. We will cache the routes by name so we can
// load them very quickly if we need to find them a second time.
foreach (static::$routes as $key => $value)
foreach (static::routes() as $key => $value)
{
if (isset($value['name']) and $value['name'] == $name)
{
......@@ -149,6 +203,40 @@ class Router {
}
}
/**
* Find the route that uses the given action and method.
*
* @param string $action
* @param string $method
* @return array
*/
public static function uses($action, $method = 'GET')
{
// If the action has already been reverse routed before, we'll just
// grab the previously found route to save time. They are cached
// in a static array on the class.
if (isset(static::$uses[$method.$action]))
{
return static::$uses[$method.$action];
}
Bundle::routes(Bundle::name($action));
foreach (static::routes() as $uri => $route)
{
// To find the route, we'll simply spin through the routes looking
// for a route with a "uses" key matching the action, then we'll
// check the request method for a match.
if (isset($route['uses']) and $route['uses'] == $action)
{
if (starts_with($uri, $method))
{
return static::$uses[$method.$action] = array($uri => $route);
}
}
}
}
/**
* Search the routes for the route matching a method and URI.
*
......@@ -192,7 +280,7 @@ class Router {
// as the destination is used to set the route's URIs.
if ($bundle !== DEFAULT_BUNDLE)
{
$uri = str_replace(Bundle::get($bundle)->handles, '', $uri);
$uri = str_replace(Bundle::option($bundle, 'handles'), '', $uri);
$uri = ltrim($uri, '/');
}
......@@ -210,7 +298,7 @@ class Router {
*/
protected static function match($destination)
{
foreach (static::$routes as $route => $action)
foreach (static::routes() as $route => $action)
{
// We only need to check routes with regular expressions since
// all other routes would have been able to be caught by the
......@@ -280,7 +368,7 @@ class Router {
// We need to grab the prefix to the bundle so we can prefix
// the route identifier with it. This informs the controller
// class out of which bundle the controller instance should
// be resolved when it is needed by the application.
// be resolved when it is needed by the app.
$prefix = Bundle::prefix($bundle);
$action = array('uses' => $prefix.$controller.'@'.$method);
......@@ -328,7 +416,7 @@ class Router {
// For optional parameters, first translate the wildcards to their
// regex equivalent, sans the ")?" ending. We'll add the endings
// back on after we know how many replacements we made.
// back on after we know the replacement count.
$key = str_replace($search, $replace, $key, $count);
if ($count > 0)
......@@ -339,4 +427,14 @@ class Router {
return strtr($key, static::$patterns);
}
/**
* Get all of the registered routes, with fallbacks at the end.
*
* @return array
*/
public static function routes()
{
return array_merge(static::$routes, static::$fallback);
}
}
\ No newline at end of file
<?php namespace Laravel;
<?php namespace Laravel; use Laravel\Routing\Route, Laravel\Routing\Router;
class URL {
......@@ -109,14 +109,29 @@ class URL {
*
* @param string $action
* @param array $parameters
* @param bool $https
* @param string $method
* @return string
*/
public static function to_action($action, $parameters = array(), $https = false)
public static function to_action($action, $parameters = array(), $method = 'GET')
{
$action = str_replace(array('.', '@'), '/', $action);
// This allows us to use true reverse routing to controllers, since
// URIs may be setup to handle the action that do not follow the
// typical Laravel controller URI convention.
$route = Router::uses($action, $method);
return static::to($action.'/'.implode('/', $parameters), $https);
if ( ! is_null($route))
{
$uri = static::explicit($route, $action, $parameters);
}
// If no route was found that handled the given action, we'll just
// generate the URL using the typical controller routing setup
// for URIs and turn SSL to false.
else
{
$uri = static::convention($action, $parameters);
}
return static::to($uri, $https);
}
/**
......@@ -126,9 +141,52 @@ class URL {
* @param array $parameters
* @return string
*/
public static function to_secure_action($action, $parameters = array())
public static function to_post_action($action, $parameters = array())
{
return static::to_action($action, $parameters, 'POST');
}
/**
* Generate a action URL from a route definition
*
* @param array $route
* @param string $action
* @param array $parameters
* @return string
*/
protected static function explicit($route, $action, $parameters)
{
$https = array_get(current($route), 'https', false);
return Route::transpose(Route::destination(key($route)), $parameters);
}
/**
* Generate an action URI by convention.
*
* @param string $action
* @param array $parameters
* @return string
*/
protected static function convention($action, $parameters)
{
return static::to_action($action, $parameters, true);
list($bundle, $action) = Bundle::parse($action);
$bundle = Bundle::get($bundle);
// If a bundle exists for the action, we will attempt to use it's "handles"
// clause as the root of the generated URL, as the bundle can only handle
// URIs that begin with that string.
$root = $bundle['handles'] ?: '';
$https = false;
// We'll replace both dots and @ signs in the URI since both are used
// to specify the controller and action, and by convention should be
// translated into URI slashes.
$uri = $root.str_replace(array('.', '@'), '/', $action);
return str_finish($uri, '/').implode('/', $parameters);
}
/**
......@@ -171,47 +229,47 @@ class URL {
* @param bool $https
* @return string
*/
public static function to_route($name, $parameters = array(), $https = false)
public static function to_route($name, $parameters = array())
{
if (is_null($route = Routing\Router::find($name)))
{
throw new \Exception("Error creating URL for undefined route [$name].");
}
$uris = explode(', ', key($route));
$uri = Route::destination(key($route));
// To determine whether the URL should be HTTPS or not, we look for the "https"
// value on the route action array. The route has control over whether the
// URL should be generated with an HTTPS protocol.
$https = array_get(current($route), 'https', false);
// Routes can handle more than one URI, but we will just take the first URI
// and use it for the URL. Since all of the URLs should point to the same
// route, it doesn't make a difference.
$uri = substr($uris[0], strpos($uris[0], '/'));
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 with spaces since
// they may not have been specified in the array of parameters.
// 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 into the method.
$uri = str_replace(array('/(:any?)', '/(:num?)'), '', $uri);
return static::to($uri, $https);
}
/**
* Generate a HTTPS URL from a route name.
*
* @param string $name
* @param array $parameters
* @return string
*/
public static function to_secure_route($name, $parameters = array())
{
return static::to_route($name, $parameters, true);
// 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