Just in case you haven't read the previous part of the series, where I explained how to build a simple URL-handling helper class, below I included the full source code for this class, along with some examples of how to use it to retrieve some of the values stored in the $_SERVER PHP array. That being said, here’s the signature of the URL helper class: class URLHelper { // constructor not implemented public function __construct(){}
// get web server root public static function get_document_root() { return $_SERVER['DOCUMENT_ROOT']; }
// get script URL public static function get_script_url() { return $_SERVER['PHP_SELF']; }
// get script query string public static function get_query_string() { return $_SERVER['QUERY_STRING']; }
// get request URI public static function get_request_uri() { return $_SERVER['REQUEST_URI']; }
// get script file name public static function get_script_filename() { return $_SERVER['SCRIPT_FILENAME']; }
// get request method public static function get_request_method() { return strtolower($_SERVER['REQUEST_METHOD']); }
} Undeniably, the definition of the above “URLHelper” class is pretty easy to follow. It's comprised of a few basic static methods, which behave like simple proxies for the aforementioned $_SERVER PHP array. That was the boring part. However, the following code samples should be a bit more interesting for you; they show how to use the helper class to generate some dynamic URLs. Here they are: echo URLHelper::get_document_root(); /* // displays the following C:/Program Files/Apache Group/Apache2/htdocs */
echo URLHelper::get_request_method(); /* displays the following get */
echo URLHelper::get_query_string(); /* displays the following fname=Alejandro&lname=Gervasio */
echo URLHelper::get_request_uri(); /* displays the following /helpers/helper_example.php */
echo URLHelper::get_script_filename(); /* displays the following C:/Program Files/Apache Group/Apache2/htdocs/helpers/helper_example.php */
echo URLHelper::get_script_url(); /* displays the following /helpers/helper_example.php */ From the previous examples, it’s clear to see that the methods of the URL helper class can be easily improved, or even better, it’s possible to add new ones that perform more useful tasks, such as generating breadcrumbs for a web page and calculating the base path of a given web site. Despite the lack of these features, this sample helper class should give you a clear idea of how to take advantage of its functionality without having to spawn an instance of it. Well, at this point you’ve surely recalled how to work with the previous URL helper, so the next thing that I’m going to do will consist of building a brand new class. This class will be responsible for validating common data, such as float and integer numbers, IP and email addresses, and so forth. If you want to learn in depth how this new helper class will be developed, you’ll have to click on the link that appears below and read the following section.
blog comments powered by Disqus |
|
|
|
|
|
|
|