Home arrow PHP arrow Page 3 - Developing a URL Handling Helper Class in PHP 5

Building a URL-handling helper class - PHP

In this third installment of a series on building helpers in PHP 5, I will provide you with the basic pointers for building a URL-handling helper class. The code examples, though simple, should serve to demonstrate how to create a helper that generates dynamic URLs.

TABLE OF CONTENTS:
  1. Developing a URL Handling Helper Class in PHP 5
  2. Review: the previous TextHelper class
  3. Building a URL-handling helper class
  4. Putting the URL helper class into action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 9
August 03, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: