Location: PHPKode > projects > GnomePHP > peec-GnomePHP-b5a360b/gnomephp/Url.php
<?php
namespace gnomephp;

use gnomephp\mvc\Router;

/**
 * 
 * Url class for handling url specific things, it can find the real link to a controller.method (args) and can also link to public resources.
 * @author peec
 *
 */
class Url{

	
	/**
	 * 
	 * Links to a specific resource in the public/ folder of the application.
	 * Use this to link to CSS/Javascript and images in the View.
	 * @param string $resource The resource relative to the public/ dir. Eg. "css/main.css"
	 * @param string $prefix If you want to prefix the resource, this should be defined.
	 */
	public function link($resource, $prefix=null){
		if (!isset($_SERVER['PATH_INFO']))$path = '';
		else $path = $_SERVER['PATH_INFO'];
		$exp = explode('/', $_SERVER['SCRIPT_NAME']);
		
		$scriptName = $exp[count($exp)-1];
		
		return str_replace('/'.$scriptName, '', substr($_SERVER['PHP_SELF'], 0, strlen($_SERVER['PHP_SELF'])-strlen($path))).'/'.$prefix.$resource;
	}
	
	
	/**
	 * 
	 * Finds the link and returns the link to a specific controller/method/args... based on the router configuration.
	 * 
	 * @param string $controller The controller name.
	 * @param string $method The method name.
	 * @param array $args array of arguments if any.
	 * @throws UrlException
	 */
	public function linkTo($controller, $method, $args=array()){
		return \gnomephp\mvc\Dispatcher::getInstance()->router->reverse($controller, $method, $args);
	}
	
	
	/**
	 * Replaces a url to urlfriendly and url secure pattern.
	 * Only ascii characters will be outputted, and if non ascii characters is found it will try to find
	 * equal character for the character.
	 * @param $str The source of the string.
	 * @param array A array of characters to replace with whitespace.
	 * @param string Delimiter for whitespaces ( default is - )
	 */
	static public function toAscii($str, $replace=array(), $delimiter='-') {
		if( !empty($replace) ) {
			$str = str_replace((array)$replace, ' ', $str);
		}

		$clean = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
		$clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
		$clean = strtolower(trim($clean, '-'));
		$clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);

		return $clean;
	}
	
	
}
Return current item: GnomePHP