Modules

URL
extends Gleez_URL
extends Kohana_URL

URL functions

package
Gleez\Helpers\URL
author
Sandeep Sangamreddi - Gleez
copyright
© 2013 Gleez Technologies
license
http://gleezcms.org/license Gleez CMS License

Class declared in MODPATH/gleez/classes/url.php on line 3.

Methods

public static canonical( mixed $url [, object $pagination = NULL , array $qstring = NULL , mixed $protocol = bool TRUE ] ) (defined in Gleez_URL)

Get the canonical URL

Parameters

  • mixed $url required - The request object or string URL
  • object $pagination = NULL - The pagination object [Optional]
  • array $qstring = NULL - The query string parameters [Optional]
  • mixed $protocol = bool TRUE - The route protocol [Optional]

Tags

Return Values

  • string

Source Code

public static function canonical($url, $pagination = NULL, $qstring = NULL, $protocol = TRUE)
{
	if ($url instanceof Request)
	{
		return URL::site($url->uri(), $protocol);
	}

	if ($pagination AND $pagination->current_page > 1)
	{
		$url .= '/p' . $pagination->current_page;
	}

	return URL::site($url, $protocol).URL::query($qstring);
}

public static explode( string $url ) (defined in Gleez_URL)

Splits url into array of it's pieces as follows: In addition it adds 'query_params' key which contains array of url-decoded key-value pairs

Parameters

  • string $url required - An URL

Return Values

  • array

Source Code

public static function explode($url)
{
	$url = parse_url($url);
	$url['query_params'] = array();

	// On seriously malformed URLs, parse_url() may return FALSE.
	if (isset($url['query']))
	{
		$pairs = explode('&', $url['query']);
		foreach($pairs as $pair)
		{
			if (trim($pair) == '')
			{
				continue;
			}

			list($sKey, $sValue) = explode('=', $pair);

			$url['query_params'][$sKey] = urldecode($sValue);
		}
	}

	return $url;
}

public static is_absolute( string $url ) (defined in Gleez_URL)

Test whether a URL is absolute

Parameters

  • string $url required - The URL to test

Return Values

  • boolean

Source Code

public static function is_absolute($url)
{
	return (strpos($url, '://') !== FALSE);
}

public static is_homepage( [ string $route_name = NULL , array $route_params = NULL ] ) (defined in Gleez_URL)

Test whether a URL is the home page

Parameters

  • string $route_name = NULL - The home route name [Optional]
  • array $route_params = NULL - The home route parameters [Optional]

Tags

Return Values

  • boolean

Source Code

public static function is_homepage($route_name = NULL, $route_params = NULL)
{
	// Process the current URL
	$request = Request::initial();
	$name = Route::name($request->route);

	$params = $request->param();
	$params['action'] = $request->action();
	$params['controller'] = $request->controller();

	$current = URL::canonical($name, $params);

	// Process the home URL
	if (empty($route_name))
	{
		$route_name = 'default';
	}
	if (empty($route_params))
	{
		$route_params = array('controller' => 'home');
	}

	$home = URL::canonical($route_name, $route_params);

	return ($current === $home);
}

public static is_remote( string $url ) (defined in Gleez_URL)

Test whether a URL is remote

Parameters

  • string $url required - The URL to test

Return Values

  • boolean

Source Code

public static function is_remote($url)
{
	return (strpos(strtolower($url), 'http://') !== FALSE)
		OR (strpos(strtolower($url), 'https://') !== FALSE)
		OR (strpos(strtolower($url), 'ftp://') !== FALSE);
}

public static sortAnchor( string $col [, boolean $reverse = bool FALSE ] ) (defined in Gleez_URL)

Create links to sort a column

Set $reverse to true to set asc as desc and vice versa.

Parameters

  • string $col required - $col
  • boolean $reverse = bool FALSE - [Optional]

Tags

Return Values

  • string

Source Code

public static function sortAnchor($col, $reverse = FALSE)
{
	$string = "";
	$orders = array('asc', 'desc');

	foreach ($orders as $order)
	{
		$class = "";
		$anchor_string = ($order == 'asc') ? "∧" : "∨";

		if ($reverse)
		{
			$order = ($order == 'asc') ? 'desc' : 'asc';
		}

		if (Arr::get($_GET, 'sort') == $col && Arr::get($_GET, 'order') == $order)
		{
			$class = "active";
		}

		$query = URL::query(array('sort' => $col, 'order' => $order));


		$string .= HTML::anchor(Request::current()->uri() . $query, $anchor_string, array('class' => $class . ' sort'));
	}

	return $string;
}

public static base( [ mixed $protocol = NULL , boolean $index = bool FALSE ] ) (defined in Kohana_URL)

Gets the base URL to the application. To specify a protocol, provide the protocol as a string or request object. If a protocol is used, a complete URL will be generated using the $_SERVER['HTTP_HOST'] variable.

// Absolute URL path with no host or protocol
echo URL::base();

// Absolute URL path with host, https protocol and index.php if set
echo URL::base('https', TRUE);

// Absolute URL path with host and protocol from $request
echo URL::base($request);

Parameters

  • mixed $protocol = NULL - Protocol string, [Request], or boolean
  • boolean $index = bool FALSE - Add index file to URL?

Tags

Return Values

  • string

Source Code

public static function base($protocol = NULL, $index = FALSE)
{
	// Start with the configured base URL
	$base_url = Kohana::$base_url;

	if ($protocol === TRUE)
	{
		// Use the initial request to get the protocol
		$protocol = Request::$initial;
	}

	if ($protocol instanceof Request)
	{
		if ( ! $protocol->secure())
		{
			// Use the current protocol
			list($protocol) = explode('/', strtolower($protocol->protocol()));
		}
		else
		{
			$protocol = 'https';
		}
	}

	if ( ! $protocol)
	{
		// Use the configured default protocol
		$protocol = parse_url($base_url, PHP_URL_SCHEME);
	}

	if ($index === TRUE AND ! empty(Kohana::$index_file))
	{
		// Add the index file to the URL
		$base_url .= Kohana::$index_file.'/';
	}

	if (is_string($protocol))
	{
		if ($port = parse_url($base_url, PHP_URL_PORT))
		{
			// Found a port, make it usable for the URL
			$port = ':'.$port;
		}

		if ($domain = parse_url($base_url, PHP_URL_HOST))
		{
			// Remove everything but the path from the URL
			$base_url = parse_url($base_url, PHP_URL_PATH);
		}
		else
		{
			// Attempt to use HTTP_HOST and fallback to SERVER_NAME
			$domain = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];
		}

		// Add the protocol and domain to the base URL
		$base_url = $protocol.'://'.$domain.$port.$base_url;
	}

	return $base_url;
}

public static query( [ array $params = NULL , boolean $use_get = bool TRUE ] ) (defined in Kohana_URL)

Merges the current GET parameters with an array of new or overloaded parameters and returns the resulting query string.

// Returns "?sort=title&limit=10" combined with any existing GET values
$query = URL::query(array('sort' => 'title', 'limit' => 10));

Typically you would use this when you are sorting query results, or something similar.

Parameters with a NULL value are left out.

Parameters

  • array $params = NULL - Array of GET parameters
  • boolean $use_get = bool TRUE - Include current request GET parameters

Return Values

  • string

Source Code

public static function query(array $params = NULL, $use_get = TRUE)
{
	if ($use_get)
	{
		if ($params === NULL)
		{
			// Use only the current parameters
			$params = $_GET;
		}
		else
		{
			// Merge the current and new parameters
			$params = Arr::merge($_GET, $params);
		}
	}

	if (empty($params))
	{
		// No query parameters
		return '';
	}

	// Note: http_build_query returns an empty string for a params array with only NULL values
	$query = http_build_query($params, '', '&');

	// Don't prepend '?' to an empty string
	return ($query === '') ? '' : ('?'.$query);
}

public static site( [ string $uri = string(0) "" , mixed $protocol = NULL , boolean $index = bool TRUE ] ) (defined in Kohana_URL)

Fetches an absolute site URL based on a URI segment.

echo URL::site('foo/bar');

Parameters

  • string $uri = string(0) "" - Site URI to convert
  • mixed $protocol = NULL - Protocol string or [Request] class to use protocol from
  • boolean $index = bool TRUE - Include the index_page in the URL

Tags

Return Values

  • string

Source Code

public static function site($uri = '', $protocol = NULL, $index = TRUE)
{
	// Chop off possible scheme, host, port, user and pass parts
	$path = preg_replace('~^[-a-z0-9+.]++://[^/]++/?~', '', trim($uri, '/'));

	if ( ! UTF8::is_ascii($path))
	{
		// Encode all non-ASCII characters, as per RFC 1738
		$path = preg_replace_callback('~([^/]+)~', 'URL::_rawurlencode_callback', $path);
	}

	// Concat the URL
	return URL::base($protocol, $index).$path;
}

public static title( string $title [, string $separator = string(1) "-" , boolean $ascii_only = bool FALSE ] ) (defined in Kohana_URL)

Convert a phrase to a URL-safe title.

echo URL::title('My Blog Post'); // "my-blog-post"

Parameters

  • string $title required - Phrase to convert
  • string $separator = string(1) "-" - Word separator (any single character)
  • boolean $ascii_only = bool FALSE - Transliterate to ASCII?

Tags

Return Values

  • string

Source Code

public static function title($title, $separator = '-', $ascii_only = FALSE)
{
	if ($ascii_only === TRUE)
	{
		// Transliterate non-ASCII characters
		$title = UTF8::transliterate_to_ascii($title);

		// Remove all characters that are not the separator, a-z, 0-9, or whitespace
		$title = preg_replace('![^'.preg_quote($separator).'a-z0-9\s]+!', '', strtolower($title));
	}
	else
	{
		// Remove all characters that are not the separator, letters, numbers, or whitespace
		$title = preg_replace('![^'.preg_quote($separator).'\pL\pN\s]+!u', '', UTF8::strtolower($title));
	}

	// Replace all separator characters and whitespace by a single separator
	$title = preg_replace('!['.preg_quote($separator).'\s]+!u', $separator, $title);

	// Trim separators from the beginning and end
	return trim($title, $separator);
}

protected static _rawurlencode_callback( array $matches ) (defined in Kohana_URL)

Callback used for encoding all non-ASCII characters, as per RFC 1738 Used by URL::site()

Parameters

  • array $matches required - Array of matches from preg_replace_callback()

Return Values

  • string - Encoded string

Source Code

protected static function _rawurlencode_callback($matches)
{
	return rawurlencode($matches[0]);
}
Documentation comments powered by Disqus