123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319 |
- <?php namespace Laravel; use Laravel\Routing\Router, Laravel\Routing\Route;
- class URL {
-
- public static $base;
-
- public static function full()
- {
- return static::to(URI::full());
- }
-
- public static function current()
- {
- return static::to(URI::current());
- }
-
- public static function home($https = null)
- {
- $route = Router::find('home');
-
-
-
- if ( ! is_null($route))
- {
- return static::to_route('home');
- }
- return static::to('/', $https);
- }
-
- public static function base()
- {
- if (isset(static::$base)) return static::$base;
- $base = 'http://localhost';
-
-
-
- if (($url = Config::get('application.url')) !== '')
- {
- $base = $url;
- }
- else
- {
- $base = Request::foundation()->getRootUrl();
- }
- return static::$base = $base;
- }
-
- public static function to($url = '', $https = null)
- {
-
-
-
- if (static::valid($url) or starts_with($url, '#'))
- {
- return $url;
- }
-
-
- if (is_null($https)) $https = Request::secure();
- $root = static::base().'/'.Config::get('application.index');
-
-
-
- if ($https and Config::get('application.ssl'))
- {
- $root = preg_replace('~http://~', 'https://', $root, 1);
- }
- else
- {
- $root = preg_replace('~https://~', 'http://', $root, 1);
- }
- return rtrim($root, '/').'/'.ltrim($url, '/');
- }
-
- public static function to_secure($url = '')
- {
- return static::to($url, true);
- }
-
- public static function to_action($action, $parameters = array())
- {
-
-
-
- $route = Router::uses($action);
- if ( ! is_null($route))
- {
- return static::explicit($route, $action, $parameters);
- }
-
-
-
- else
- {
- return static::convention($action, $parameters);
- }
- }
-
- protected static function explicit($route, $action, $parameters)
- {
- $https = array_get(current($route), 'https', null);
- return static::to(static::transpose(key($route), $parameters), $https);
- }
-
- protected static function convention($action, $parameters)
- {
- list($bundle, $action) = Bundle::parse($action);
- $bundle = Bundle::get($bundle);
-
-
-
- $root = $bundle['handles'] ?: '';
- $parameters = implode('/', $parameters);
-
-
-
- $uri = $root.'/'.str_replace(array('.', '@'), '/', $action);
- $uri = static::to(str_finish($uri, '/').$parameters);
- return trim($uri, '/');
- }
-
- public static function to_asset($url, $https = null)
- {
- if (static::valid($url)) return $url;
-
-
-
- if ($root = Config::get('application.asset_url', false))
- {
- return rtrim($root, '/').'/'.ltrim($url, '/');
- }
- $url = static::to($url, $https);
-
-
-
- if (($index = Config::get('application.index')) !== '')
- {
- $url = str_replace($index.'/', '', $url);
- }
- return $url;
- }
-
- 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].");
- }
-
-
-
- $https = array_get(current($route), 'https', null);
- $uri = trim(static::transpose(key($route), $parameters), '/');
- return static::to($uri, $https);
- }
-
- public static function transpose($uri, $parameters)
- {
-
-
-
- foreach ((array) $parameters as $parameter)
- {
- if ( ! is_null($parameter))
- {
- $uri = preg_replace('/\(.+?\)/', $parameter, $uri, 1);
- }
- }
-
-
-
- $uri = preg_replace('/\(.+?\)/', '', $uri);
- return trim($uri, '/');
- }
-
- public static function valid($url)
- {
- return filter_var($url, FILTER_VALIDATE_URL) !== false;
- }
- }
|