Sådan benyttes komponenten Url klassen
Først skal du inkludere den fil der beskriver komponenten, som en klasse fil
<? require_once(HTML_PACKAGE_PATH.'/Url.php'); ?>
Dernæst kan du enten benytte komponenten som et taglib (statiske metoder):
<? Url::display($param1, $param2, $param3, ...); ?>
eller du kan lave en instance af komponenten og benytte metoderne direkte:
<? $object = new Url($param1, $param2, $param3, ...); print $object->getHtml(); ?>
Sådan vises komponenten Url klassen
Den fulde PHP kildekode for Url klassen
<?php/** * @package util * @see HTML_UTIL_COMPONENT_PATH.'/Url.php' * @copyright (c) http://Finn-Rasmussen.com * @license http://Finn-Rasmussen.com/license/ myPHP License conditions * @author http://Finn-Rasmussen.com * @version 1.11 * @since 27-nov-2009 *//** * The required files */require_once(HTML_UTIL_COMPONENT_PATH.'/Server.php');require_once(HTML_BASE_UTIL_PATH.'/Link.php');if (defined('HTML_BASIC_UTIL_PATH')) { require_once(HTML_BASIC_UTIL_PATH.'/Message.php');}if (defined('HTML_LOG_UTIL_PATH')) { require_once(HTML_LOG_UTIL_PATH.'/Log.php');}if (defined('HTML_LANGUAGE_UTIL_PATH')) { require_once(HTML_LANGUAGE_UTIL_PATH.'/Translate.php');}/** * Get the url or Link from the specified key * <code> * Usage: * $key = 'URL_HOME'; * $url = new Url(); * print $url->get($key); * print $url->link($key); * Or * $fileName = '/jquery.js'; * $filePath = HTML_JQUERY_PATH; * $packageName = MYPHP_JQUERY_NAME; * $packagePath = HTML_JQUERY_PATH; * $src = Url::src($fileName, $filePath, $packageName, $packagePath); * Or * print Url::get($key); * Or * print Url::link($key); * </code> * @package util */class Url { /** * Constructor */ function __construct() { } /** * Get the SRC to the url for the *.js or *.gif or *.css files * <code> * Usage: * $fileName = '/jquery.js'; * $filePath = HTML_JQUERY_PATH; * $packageName = MYPHP_JQUERY_NAME; * $packagePath = HTML_JQUERY_PATH; * $src = Url::src($fileName, $filePath, $packageName, $packagePath); * </code> * @param String $fileName The name of the javascript file. I.e. '/foo.js' * @param String $filePath The path to the javascript file. I.e. '/js' * @param String $packageName The name of the package, where the javascript file is located * @param String $packagePath The Path to the package, where the javascript file is located * @return String The calculated SRC for the src include files */ public static function src($fileName, $filePath='', $packageName='', $packagePath='') { $url = PROJECT_URL; if ($packageName != '') { $url = $url.MYPHP_PREFIX.CURRENT_VERSION; $url = $url.'/'.basename($packageName); } if ($packagePath != '') { $url = $url.'/'.basename($packagePath); } if ($filePath != '') { $url = $url.'/'.basename($filePath); } if ($fileName != '') { $url = $url.'/'.basename($fileName); } return $url; } /** * Returns the full link for the specified key * <code> * Usage: * $key = 'URL_HOME'; * $url = new Url(); * print $url->link($key); * Or * print Url::link($key); * </code> * @static * @param String $key The key to the url to get * @return String the complete link or '' if not found */ public static function link($key) { $html = ''; $href = Url::get($key); if ($href != null) { $text = $href; if (defined('HTML_LANGUAGE_UTIL_PATH')) { $text = Translate::get($text); } /** * TODO how to specify the relative url ? */ $link = new Link($text, $href); $html .= $link->getHtml(); } else { $msg = "Url Not found in GLOBALS[DEFINE_URL], key:$key"; if (defined('DEBUG_LEVEL_SHOW_INFO') && DEBUG_LEVEL & DEBUG_LEVEL_SHOW_INFO) { Log::error($msg, __FILE__, __LINE__); } if (defined('HTML_BASIC_UTIL_PATH')) { Message::add($msg, __FILE__, __LINE__); } } return $html; } /** * Returns the url for the specified key * @see HTML_BASE_RESOURCE_PATH/Define.php * <code> * Usage: * $key = 'URL_HOME'; * $url = new Url(); * print $url->get($key); * Or * print Url::get($key); * </code> * @static * @param String $key The key to the url to get * @return String the complete url or null if not found */ public static function get($key) { $url = null; $theKey = md5($key); if (array_key_exists($theKey, $GLOBALS[DEFINE_URL])) { $url = $GLOBALS[DEFINE_URL][$theKey]; } else { $msg = "Key not Not found in GLOBALS[DEFINE_URL], key:$key - md5:$theKey"; if (defined('DEBUG_LEVEL_SHOW_INFO') && DEBUG_LEVEL & DEBUG_LEVEL_SHOW_INFO) { Log::error($msg, __FILE__, __LINE__); } if (defined('HTML_BASIC_UTIL_PATH')) { Message::add($msg, __FILE__, __LINE__); } } return $url; } /** * Get the real subdomain name, if the domain is a subdomain * or return domain if a real domain name * Special handling for my own subdomains * sample: test.eksperter.dk => eksperter.dk * <code> * Usage: * $domain = 'vin.eksperter.dk'; * $subdomain = Url::subdomain($domain); * print $subdomain; // eksperter.dk * Or * $domain = 'http://vin.eksperter.dk'; * $subdomain = Url::subdomain($domain); * print $subdomain; // eksperter.dk * </code> * @static * @param String $subdomain The subdomain name. Escape special chars like '.' * @return String The real domain name, stripped away the subdomain or empty "" */ public static function subdomain($subdomain) { $port = Server::getServerPort(); if ($port != '80') { $port = ":$port"; } else { $port = ''; } $strHttp = 'http:/'.'/'; $domainname = $subdomain; $names = explode("\.", $subdomain); /** * 0 1 2 * http://vin . eksperter . dk */ if (count($names) == 3) { $tempdomainname = $names[1].'.'.$names[2]; if ($subdomain != $tempdomainname) { $domainname = $tempdomainname; } } else { // TODO What ? } return $strHttp.$domainname.$port; }}?>
Den fulde HTML kildekode for Url klassen
<? Der er ikke fundet noget ?>
Her er 'klasse metoderne' for Url klassen:
Her er 'objekt variable' for Url klassen: