Files
spa-api/laravel/url.php

337 lines
8.7 KiB
PHP
Raw Normal View History

2012-02-12 14:48:36 -06:00
<?php namespace Laravel; use Laravel\Routing\Router, Laravel\Routing\Route;
2011-06-08 23:45:08 -05:00
class URL {
2012-02-04 10:58:38 -06:00
/**
* The cached base URL.
*
* @var string
*/
public static $base;
2012-02-12 14:48:36 -06:00
/**
* Get the full URI including the query string.
*
* @return string
*/
public static function full()
{
return static::to(URI::full());
}
2012-02-07 13:31:08 -06:00
/**
* Get the full URL for the current request.
*
* @return string
*/
public static function current()
{
2012-08-20 10:46:23 -05:00
return static::to(URI::current(), null, false, false);
2012-02-07 13:31:08 -06:00
}
2012-02-15 08:27:10 -06:00
/**
* Get the URL for the application root.
*
* @param bool $https
* @return string
*/
public static function home($https = null)
2012-02-15 08:27:10 -06:00
{
$route = Router::find('home');
// If a route named "home" exists, we'll route to that instead of using
// the single slash root URI. This allows the HTTPS attribute to be
2012-02-15 08:27:10 -06:00
// respected instead of being hard-coded in the redirect.
if ( ! is_null($route))
{
return static::to_route('home');
}
return static::to('/', $https);
}
/**
* Get the base URL of the application.
*
* @return string
*/
public static function base()
{
2012-02-04 10:58:38 -06:00
if (isset(static::$base)) return static::$base;
$base = 'http://localhost';
2012-04-04 10:16:15 -05:00
// If the application's URL configuration is set, we will just use that
2012-02-12 14:48:36 -06:00
// instead of trying to guess the URL from the $_SERVER array's host
2012-04-04 11:08:14 -05:00
// and script variables as this is a more reliable method.
2012-02-04 10:58:38 -06:00
if (($url = Config::get('application.url')) !== '')
{
$base = $url;
}
2012-04-02 20:45:20 -05:00
else
{
$base = Request::foundation()->getRootUrl();
2012-02-23 16:18:08 -06:00
}
return static::$base = $base;
}
2011-06-08 23:45:08 -05:00
/**
* Generate an application URL.
*
2011-09-22 00:42:44 -05:00
* <code>
* // Create a URL to a location within the application
* $url = URL::to('user/profile');
2011-11-09 21:55:21 -06:00
*
* // Create a HTTPS URL to a location within the application
* $url = URL::to('user/profile', true);
2011-09-22 00:42:44 -05:00
* </code>
*
2011-06-08 23:45:08 -05:00
* @param string $url
* @param bool $https
2012-08-20 10:46:23 -05:00
* @param bool $asset
* @param bool $locale
2011-06-08 23:45:08 -05:00
* @return string
*/
2012-08-20 10:46:23 -05:00
public static function to($url = '', $https = null, $asset = false, $locale = true)
2011-06-08 23:45:08 -05:00
{
2012-04-02 13:02:49 -05:00
// If the given URL is already valid or begins with a hash, we'll just return
// the URL unchanged since it is already well formed. Otherwise we will add
// the base URL of the application and return the full URL.
if (static::valid($url) or starts_with($url, '#'))
{
return $url;
}
2011-06-08 23:45:08 -05:00
// Unless $https is specified (true or false), we maintain the current request
// security for any new links generated. So https for all secure links.
if (is_null($https)) $https = Request::secure();
2012-08-20 10:46:23 -05:00
$root = static::base();
if ( ! $asset)
{
$root .= '/'.Config::get('application.index');
}
$languages = Config::get('application.languages');
if ( ! $asset and $locale and count($languages) > 0)
2012-08-20 10:46:23 -05:00
{
if (in_array($default = Config::get('application.language'), $languages))
{
$root = rtrim($root, '/').'/'.$default;
}
2012-08-20 10:46:23 -05:00
}
2011-06-08 23:45:08 -05:00
2012-01-16 13:59:24 -06:00
// Since SSL is not often used while developing the application, we allow the
2011-11-11 22:06:51 -06:00
// developer to disable SSL on all framework generated links to make it more
2012-01-16 13:59:24 -06:00
// convenient to work with the site while developing locally.
if ($https and Config::get('application.ssl'))
2011-10-04 21:43:39 -05:00
{
2011-11-09 21:55:21 -06:00
$root = preg_replace('~http://~', 'https://', $root, 1);
2011-10-04 21:43:39 -05:00
}
2012-04-04 11:08:14 -05:00
else
{
$root = preg_replace('~https://~', 'http://', $root, 1);
}
2011-06-08 23:45:08 -05:00
2011-11-09 21:55:21 -06:00
return rtrim($root, '/').'/'.ltrim($url, '/');
2011-06-08 23:45:08 -05:00
}
/**
* Generate an application URL with HTTPS.
*
* @param string $url
* @return string
*/
public static function to_secure($url = '')
2011-06-08 23:45:08 -05:00
{
return static::to($url, true);
2011-06-08 23:45:08 -05:00
}
/**
* Generate a URL to a controller action.
*
* <code>
* // Generate a URL to the "index" method of the "user" controller
* $url = URL::to_action('user@index');
*
* // Generate a URL to http://example.com/user/profile/taylor
* $url = URL::to_action('user@profile', array('taylor'));
* </code>
*
* @param string $action
* @param array $parameters
* @return string
*/
2012-02-12 14:48:36 -06:00
public static function to_action($action, $parameters = array())
{
2012-02-09 23:35:08 -06:00
// This allows us to use true reverse routing to controllers, since
// URIs may be setup to handle the action that do not follow the
2012-02-12 14:48:36 -06:00
// typical Laravel controller URI conventions.
$route = Router::uses($action);
2012-02-09 23:35:08 -06:00
if ( ! is_null($route))
{
2012-02-12 14:48:36 -06:00
return static::explicit($route, $action, $parameters);
2012-02-09 23:35:08 -06:00
}
// If no route was found that handled the given action, we'll just
// generate the URL using the typical controller routing setup
2012-04-04 11:09:44 -05:00
// for URIs and turn SSL to false by default.
2012-02-09 23:35:08 -06:00
else
{
2012-02-12 14:48:36 -06:00
return static::convention($action, $parameters);
2012-02-09 23:35:08 -06:00
}
}
/**
* Generate an action URL from a route definition
2012-02-09 23:35:08 -06:00
*
* @param array $route
* @param string $action
* @param array $parameters
* @return string
*/
protected static function explicit($route, $action, $parameters)
{
$https = array_get(current($route), 'https', null);
2012-02-09 23:35:08 -06:00
2012-02-12 14:48:36 -06:00
return static::to(static::transpose(key($route), $parameters), $https);
2012-02-09 23:35:08 -06:00
}
/**
* Generate an action URI by convention.
*
* @param string $action
* @param array $parameters
* @return string
*/
protected static function convention($action, $parameters)
{
2012-02-09 23:35:08 -06:00
list($bundle, $action) = Bundle::parse($action);
$bundle = Bundle::get($bundle);
// If a bundle exists for the action, we will attempt to use its "handles"
2012-02-09 23:35:08 -06:00
// clause as the root of the generated URL, as the bundle can only handle
2012-02-23 16:19:00 -06:00
// URIs that begin with that string and no others.
2012-02-09 23:35:08 -06:00
$root = $bundle['handles'] ?: '';
2012-02-12 14:48:36 -06:00
$parameters = implode('/', $parameters);
2012-02-09 23:35:08 -06:00
// 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
2012-02-23 14:02:59 -06:00
// translated into URI slashes for the URL.
2012-02-12 14:48:36 -06:00
$uri = $root.'/'.str_replace(array('.', '@'), '/', $action);
2012-02-09 23:35:08 -06:00
2012-02-12 14:48:36 -06:00
$uri = static::to(str_finish($uri, '/').$parameters);
return trim($uri, '/');
}
2011-06-08 23:45:08 -05:00
/**
2012-01-16 13:59:24 -06:00
* Generate an application URL to an asset.
2011-09-22 00:42:44 -05:00
*
2012-01-16 13:59:24 -06:00
* @param string $url
* @param bool $https
2011-06-08 23:45:08 -05:00
* @return string
*/
2012-01-16 13:59:24 -06:00
public static function to_asset($url, $https = null)
2011-06-08 23:45:08 -05:00
{
2012-04-23 23:43:09 -05:00
if (static::valid($url)) return $url;
// If a base asset URL is defined in the configuration, use that and don't
// try and change the HTTP protocol. This allows the delivery of assets
// through a different server or third-party content delivery network.
if ($root = Config::get('application.asset_url', false))
{
2012-04-23 23:43:09 -05:00
return rtrim($root, '/').'/'.ltrim($url, '/');
}
2012-08-20 10:46:23 -05:00
$url = static::to($url, $https, true);
2011-06-08 23:45:08 -05:00
2012-01-16 13:59:24 -06:00
// 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
2012-02-12 14:48:36 -06:00
// in the application config from the generated URL.
2012-01-16 13:59:24 -06:00
if (($index = Config::get('application.index')) !== '')
{
$url = str_replace($index.'/', '', $url);
}
2011-06-08 23:45:08 -05:00
2012-01-16 13:59:24 -06:00
return $url;
2011-06-08 23:45:08 -05:00
}
/**
2012-01-16 13:59:24 -06:00
* Generate a URL from a route name.
2011-09-22 00:42:44 -05:00
*
* <code>
* // Create a URL to the "profile" named route
2012-01-16 13:59:24 -06:00
* $url = URL::to_route('profile');
2011-09-22 00:42:44 -05:00
*
2012-01-16 13:59:24 -06:00
* // Create a URL to the "profile" named route with wildcard parameters
* $url = URL::to_route('profile', array($username));
2011-09-22 00:42:44 -05:00
* </code>
2012-01-16 13:59:24 -06:00
*
* @param string $name
* @param array $parameters
* @return string
2011-06-08 23:45:08 -05:00
*/
2012-02-09 23:35:08 -06:00
public static function to_route($name, $parameters = array())
2011-06-08 23:45:08 -05:00
{
2012-01-16 13:59:24 -06:00
if (is_null($route = Routing\Router::find($name)))
2011-06-08 23:45:08 -05:00
{
2012-01-16 13:59:24 -06:00
throw new \Exception("Error creating URL for undefined route [$name].");
2011-06-08 23:45:08 -05:00
}
2012-02-09 23:35:08 -06:00
// To determine whether the URL should be HTTPS or not, we look for the "https"
2012-02-12 14:48:36 -06:00
// 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', null);
2012-01-16 13:59:24 -06:00
2012-02-12 19:26:16 -06:00
$uri = trim(static::transpose(key($route), $parameters), '/');
return static::to($uri, $https);
2012-02-12 14:48:36 -06:00
}
/**
* 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 to us.
$uri = preg_replace('/\(.+?\)/', '', $uri);
2012-02-12 14:48:36 -06:00
return trim($uri, '/');
2012-01-16 13:59:24 -06:00
}
2012-04-02 13:02:49 -05:00
/**
* Determine if the given URL is valid.
*
* @param string $url
* @return bool
*/
public static function valid($url)
{
return filter_var($url, FILTER_VALIDATE_URL) !== false;
}
2012-07-27 13:52:15 +01:00
}