making changes to routing.
This commit is contained in:
122
laravel/url.php
122
laravel/url.php
@@ -1,4 +1,4 @@
|
||||
<?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, true);
|
||||
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)
|
||||
{
|
||||
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));
|
||||
|
||||
// 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], '/'));
|
||||
// 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);
|
||||
|
||||
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)
|
||||
{
|
||||
$uri = preg_replace('/\(.+?\)/', $parameter, $uri, 1);
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user