Monday, 29 August 2011

Accessing HTTP Header Information With Zend Framework

By Chris Channing


Zend Framework is a popular web development language based on PHP. It allows developers to quickly access vital information on environment variables and HTTP request information at all parts of the application.

Zend Framework makes use of several different design patterns. One of them is the singleton design pattern. This design can be simplified by thinking of it as a global variable: it can be accessed and changed at any time. Normally this practice is frowned upon because it leads to poor code, but the Zend Framework front controller makes use of this to provide HTTP request information to the developer at any point in the framework stack. It's one of the few times this pattern is considered acceptable.

If you do intend on getting URL parameters in a view, you have two options. The easiest is to use a view helper. The Server Url view helper allows developers to quickly access the current URL and URI. Other information can be accessed by passing the variables from your controller to your view. While technically you can still access the front controller, it goes against the entire design of Zend Framework to do so.

The controller extends the basic Zend Controller Action. With this class you can access HTTP request information through several different methods. You are able to access the scheme, domain, controller, action, path information, and parameters if they are present. You will have to piece all of these variables together to build a URI. It's more work than in the view because we don't use the view helper here.

The controller makes it easy to access the HTTP request because it extends a class that contains the request object. Library files will more than likely not do the same: you will have to create a new instance of the Zend Front Controller to get the information. As talked about earlier, this object is a singleton so you are able to access it anywhere at all. While very useful, this type of power should be used accordingly.

To make things easier in getting a base URL, you may use a base URL helper. This allows you to quickly access a set base URL in any part of the application. You set this helper in the bootstrap and in your application configuration file. The base URL helper takes more time in setting up, but it's a joy to have when working in the view to quickly piece together URL information.

In Conclusion

Built on PHP, it's natural that Zend Framework offers everything that PHP does in regards to getting URI information. The only difference is that Zend Framework makes it easier to access these objects and also does so in a consistent manner. Just remember to use the proper methods of accessing the information so you don't violate the Zend Framework rule set.




About the Author:



No comments:

Post a Comment