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

Putting the URL helper class into action - 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

Now that there's a URL helper class available for testing purposes, it'd be useful to see how it can be put into action, right? With that idea in mind, below I coded a group of examples that show precisely how to use the helper for retrieving URL-related information about a PHP script running on a Windows web server.

Here are the corresponding code samples:

$urlhelper = new URLHelper();

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

*/

As you can see above, the methods of the URL helper class have been utilized for retrieving some typical values about the script being executed, such as the request URI and the script's URL, the root of the web server and the eventual query string, and so forth, assuming that these parameters are available to be fetched from the web host.

Again, it's feasible to add more methods to the helper that perform more complicated tasks, but I guess the class in its current version should be enough to demonstrate how to create a URL helper in PHP 5.

Finally, feel free to edit all of the examples shown in this tutorial, particularly if you're planning to include different kinds of helper classes in your own framework.

Final thoughts

That's about it. In this third installment of the series, I provided you with the basic pointers for building a URL-handling helper class. Of course, the methods of this sample class should be improved to fit the requirements of production environments, but they should serve to demonstrate how to create a helper that generates dynamic URLs.

In the next article, I'm going to enhance the signature of this class by declaring its methods static, which is pretty similar to the process covered in the preceding tutorial.

Don't miss the upcoming tutorial!



 
 
>>> 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: