In reality, building a helper class that generates URLs dynamically is normally a no-brainer process, but in this specific case it'll be even simpler. The class that I'm planning to develop will be a simple wrapper for most of the elements of the $_SERVER superglobal PHP array. Now that I've explained how this URL-handling helper class is going to work, I suggest that you look at its signature, which has been listed below. Here it is: class URLHelper { // constructor not implemented public function __construct(){}
// get web server root public function get_document_root() { return $_SERVER['DOCUMENT_ROOT']; }
// get script URL public function get_script_url() { return $_SERVER['PHP_SELF']; }
// get script query string public function get_query_string() { return $_SERVER['QUERY_STRING']; }
// get request URI public function get_request_uri() { return $_SERVER['REQUEST_URI']; }
// get script file name public function get_script_filename() { return $_SERVER['SCRIPT_FILENAME']; }
// get request method public function get_request_method() { return strtolower($_SERVER['REQUEST_METHOD']); } } Certainly, the definition of the above "URLHelper" class confirms exactly what I said a few lines before, because its methods are simple proxies for many of the values stored on the native $_SERVER PHP array. Naturally, it's possible to implement more complex methods, for instance, for building the breadcrumbs for a web site, but for the moment I'm going to keep the functionality of this URL helper class rather simple. Having shown how this sample class looks, I'm going to code a few hands-on examples to demonstrate how to perform some basic tasks using some of the class's methods. As usual, to see how these examples will be created, please read the section to come.
blog comments powered by Disqus |
|
|
|
|
|
|
|