updated routing to fix several issues.

This commit is contained in:
Taylor Otwell
2012-02-12 14:48:36 -06:00
parent 31cf44c374
commit 3a92facc76
31 changed files with 960 additions and 772 deletions

View File

@@ -1,4 +1,4 @@
<?php namespace Laravel; use Laravel\Routing\Route, Laravel\Routing\Router;
<?php namespace Laravel; use Laravel\Routing\Router, Laravel\Routing\Route;
class URL {
@@ -9,6 +9,16 @@ class URL {
*/
public static $base;
/**
* Get the full URI including the query string.
*
* @return string
*/
public static function full()
{
return static::to(URI::full());
}
/**
* Get the full URL for the current request.
*
@@ -30,9 +40,9 @@ class URL {
$base = 'http://localhost';
// If the application URL configuration is set, we will just use
// that instead of trying to guess the URL based on the $_SERVER
// array's host and script name.
// If the application URL configuration is set, we will just use that
// instead of trying to guess the URL from the $_SERVER array's host
// and script variables as this is more reliable.
if (($url = Config::get('application.url')) !== '')
{
$base = $url;
@@ -43,10 +53,14 @@ class URL {
// Basically, by removing the basename, we are removing everything after the
// and including the front controller from the request URI. Leaving us with
// the path in which the framework is installed. From that path, we can
// construct the base URL to the application.
$path = str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
// the path in which the framework is installed.
$script = $_SERVER['SCRIPT_NAME'];
$path = str_replace(basename($script), '', $script);
// Now that we have the base URL, all we need to do is attach the protocol
// and the HTTP_HOST to build the full URL for the application. We also
// trim off trailing slashes to clean the URL.
$base = rtrim($protocol.$_SERVER['HTTP_HOST'].$path, '/');
}
@@ -109,41 +123,26 @@ class URL {
*
* @param string $action
* @param array $parameters
* @param string $method
* @return string
*/
public static function to_action($action, $parameters = array(), $method = 'GET')
public static function to_action($action, $parameters = array())
{
// 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);
// typical Laravel controller URI conventions.
$route = Router::uses($action);
if ( ! is_null($route))
{
$uri = static::explicit($route, $action, $parameters);
return 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::convention($action, $parameters);
}
return static::to($uri, $https);
}
/**
* Generate a HTTPS URL to a controller action.
*
* @param string $action
* @param array $parameters
* @return string
*/
public static function to_post_action($action, $parameters = array())
{
return static::to_action($action, $parameters, 'POST');
}
/**
@@ -158,7 +157,7 @@ class URL {
{
$https = array_get(current($route), 'https', false);
return Route::transpose(Route::destination(key($route)), $parameters);
return static::to(static::transpose(key($route), $parameters), $https);
}
/**
@@ -181,12 +180,16 @@ class URL {
$https = false;
$parameters = implode('/', $parameters);
// 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);
$uri = $root.'/'.str_replace(array('.', '@'), '/', $action);
return str_finish($uri, '/').implode('/', $parameters);
$uri = static::to(str_finish($uri, '/').$parameters);
return trim($uri, '/');
}
/**
@@ -204,7 +207,7 @@ class URL {
// 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
// in the application configuration from the URL.
// in the application config from the generated URL.
if (($index = Config::get('application.index')) !== '')
{
$url = str_replace($index.'/', '', $url);
@@ -236,14 +239,40 @@ class URL {
throw new \Exception("Error creating URL for undefined route [$name].");
}
$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.
// value on the route action array. The route has control over whether the URL
// should be generated with an HTTPS protocol string or just HTTP.
$https = array_get(current($route), 'https', false);
return static::to(Route::transpose($uri, $parameters), $https);
return static::to(static::transpose(key($route), $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.
$uri = str_replace(array_keys(Router::$optional), '', $uri);
return trim($uri, '/');
}
}