merged skunkworks into develop.
This commit is contained in:
198
laravel/url.php
198
laravel/url.php
@@ -5,24 +5,20 @@ class URL {
|
||||
/**
|
||||
* Get the base URL of the application.
|
||||
*
|
||||
* If the application URL is explicitly defined in the application configuration
|
||||
* file, that URL will be returned. Otherwise, the URL will be guessed based on
|
||||
* the host and script name available in the global $_SERVER array.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function base()
|
||||
{
|
||||
if (($base = Config::$items['application']['url']) !== '') return $base;
|
||||
if (($base = Config::get('application.url')) !== '') return $base;
|
||||
|
||||
if (isset($_SERVER['HTTP_HOST']))
|
||||
{
|
||||
$protocol = (Request::secure()) ? 'https://' : 'http://';
|
||||
|
||||
// By removing the basename of the script, we should be left with the path
|
||||
// in which the framework is installed. For example, if the framework is
|
||||
// installed to http://localhost/laravel/public, the path we'll get from
|
||||
// this statement will be "/laravel/public".
|
||||
// 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']);
|
||||
|
||||
return rtrim($protocol.$_SERVER['HTTP_HOST'].$path, '/');
|
||||
@@ -50,12 +46,12 @@ class URL {
|
||||
{
|
||||
if (filter_var($url, FILTER_VALIDATE_URL) !== false) return $url;
|
||||
|
||||
$root = static::base().'/'.Config::$items['application']['index'];
|
||||
$root = static::base().'/'.Config::get('application.index');
|
||||
|
||||
// Since SSL is often not used while developing the application, we allow the
|
||||
// Since SSL is not often used while developing the application, we allow the
|
||||
// developer to disable SSL on all framework generated links to make it more
|
||||
// convenient to work with the site while developing.
|
||||
if ($https and Config::$items['application']['ssl'])
|
||||
// convenient to work with the site while developing locally.
|
||||
if ($https and Config::get('application.ssl'))
|
||||
{
|
||||
$root = preg_replace('~http://~', 'https://', $root, 1);
|
||||
}
|
||||
@@ -74,86 +70,6 @@ class URL {
|
||||
return static::to($url, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate an application URL to an asset.
|
||||
*
|
||||
* @param string $url
|
||||
* @param bool $https
|
||||
* @return string
|
||||
*/
|
||||
public static function to_asset($url, $https = null)
|
||||
{
|
||||
if (is_null($https)) $https = Request::secure();
|
||||
|
||||
$url = static::to($url, $https);
|
||||
|
||||
// Since assets are not served by Laravel, we do not need to come through
|
||||
// the front controller. We'll remove the application index specified in
|
||||
// the application configuration from the generated URL.
|
||||
if (($index = Config::$items['application']['index']) !== '')
|
||||
{
|
||||
$url = str_replace($index.'/', '', $url);
|
||||
}
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a URL from a route name.
|
||||
*
|
||||
* For routes that have wildcard parameters, an array may be passed as the
|
||||
* second parameter to the method. The values of this array will be used to
|
||||
* fill the wildcard segments of the route URI.
|
||||
*
|
||||
* <code>
|
||||
* // Create a URL to the "profile" named route
|
||||
* $url = URL::to_route('profile');
|
||||
*
|
||||
* // Create a URL to the "profile" named route with wildcard parameters
|
||||
* $url = URL::to_route('profile', array($username));
|
||||
* </code>
|
||||
*
|
||||
* @param string $name
|
||||
* @param array $parameters
|
||||
* @param bool $https
|
||||
* @return string
|
||||
*/
|
||||
public static function to_route($name, $parameters = array(), $https = false)
|
||||
{
|
||||
if ( ! is_null($route = IoC::core('routing.router')->find($name)))
|
||||
{
|
||||
$uris = explode(', ', key($route));
|
||||
|
||||
$uri = substr($uris[0], strpos($uris[0], '/'));
|
||||
|
||||
// Spin through each route parameter and replace the route wildcard
|
||||
// segment with the corresponding parameter passed to the method.
|
||||
// Afterwards, we will replace all of the remaining optional URI
|
||||
// segments with spaces since they may not have been specified
|
||||
// in the array of parameters.
|
||||
foreach ((array) $parameters as $parameter)
|
||||
{
|
||||
$uri = preg_replace('/\(.+?\)/', $parameter, $uri, 1);
|
||||
}
|
||||
|
||||
return static::to(str_replace(array('/(:any?)', '/(:num?)'), '', $uri), $https);
|
||||
}
|
||||
|
||||
throw new \OutOfBoundsException("Error creating URL for undefined route [$name].");
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a URL to a controller action.
|
||||
*
|
||||
@@ -180,14 +96,8 @@ class URL {
|
||||
/**
|
||||
* Generate a HTTPS URL to a controller action.
|
||||
*
|
||||
* <code>
|
||||
* // Generate a HTTPS URL to the "index" method of the "user" controller
|
||||
* $url = URL::to_action('user@index');
|
||||
* </code>
|
||||
*
|
||||
* @param string $action
|
||||
* @param array $parameters
|
||||
* @param bool $https
|
||||
* @return string
|
||||
*/
|
||||
public static function to_secure_action($action, $parameters = array())
|
||||
@@ -196,62 +106,86 @@ class URL {
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a URL friendly "slug".
|
||||
* Generate an application URL to an asset.
|
||||
*
|
||||
* <code>
|
||||
* // Returns "this-is-my-blog-post"
|
||||
* $slug = URL::slug('This is my blog post!');
|
||||
*
|
||||
* // Returns "this_is_my_blog_post"
|
||||
* $slug = URL::slug('This is my blog post!', '_');
|
||||
* </code>
|
||||
*
|
||||
* @param string $title
|
||||
* @param string $separator
|
||||
* @param string $url
|
||||
* @param bool $https
|
||||
* @return string
|
||||
*/
|
||||
public static function slug($title, $separator = '-')
|
||||
public static function to_asset($url, $https = null)
|
||||
{
|
||||
$title = Str::ascii($title);
|
||||
if (is_null($https)) $https = Request::secure();
|
||||
|
||||
// Remove all characters that are not the separator, letters, numbers, or whitespace.
|
||||
$title = preg_replace('![^'.preg_quote($separator).'\pL\pN\s]+!u', '', Str::lower($title));
|
||||
$url = static::to($url, $https);
|
||||
|
||||
// Replace all separator characters and whitespace by a single separator
|
||||
$title = preg_replace('!['.preg_quote($separator).'\s]+!u', $separator, $title);
|
||||
// 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 generated URL.
|
||||
if (($index = Config::get('application.index')) !== '')
|
||||
{
|
||||
$url = str_replace($index.'/', '', $url);
|
||||
}
|
||||
|
||||
return trim($title, $separator);
|
||||
return $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic Method for dynamically creating URLs to named routes.
|
||||
* Generate a URL from a route name.
|
||||
*
|
||||
* <code>
|
||||
* // Create a URL to the "profile" named route
|
||||
* $url = URL::to_profile();
|
||||
* $url = URL::to_route('profile');
|
||||
*
|
||||
* // Create a URL to the "profile" named route with wildcard segments
|
||||
* $url = URL::to_profile(array($username));
|
||||
*
|
||||
* // Create a URL to the "profile" named route using HTTPS
|
||||
* $url = URL::to_secure_profile();
|
||||
* // Create a URL to the "profile" named route with wildcard parameters
|
||||
* $url = URL::to_route('profile', array($username));
|
||||
* </code>
|
||||
*
|
||||
* @param string $name
|
||||
* @param array $parameters
|
||||
* @param bool $https
|
||||
* @return string
|
||||
*/
|
||||
public static function __callStatic($method, $parameters)
|
||||
public static function to_route($name, $parameters = array(), $https = false)
|
||||
{
|
||||
$parameters = (isset($parameters[0])) ? $parameters[0] : array();
|
||||
|
||||
if (strpos($method, 'to_secure_') === 0)
|
||||
if (is_null($route = Routing\Router::find($name)))
|
||||
{
|
||||
return static::to_route(substr($method, 10), $parameters, true);
|
||||
throw new \Exception("Error creating URL for undefined route [$name].");
|
||||
}
|
||||
|
||||
if (strpos($method, 'to_') === 0)
|
||||
$uris = explode(', ', 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], '/'));
|
||||
|
||||
// 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.
|
||||
foreach ((array) $parameters as $parameter)
|
||||
{
|
||||
return static::to_route(substr($method, 3), $parameters);
|
||||
$uri = preg_replace('/\(.+?\)/', $parameter, $uri, 1);
|
||||
}
|
||||
|
||||
throw new \BadMethodCallException("Method [$method] is not defined on the URL class.");
|
||||
// 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);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user