Chủ Nhật, 28 tháng 8, 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.

The view of a Zend Framework application is where all template code goes. Due to its nature, it's not acceptable to put logic code here. Because we aren't able to use the HTTP request singleton, we instead have access to a view helper. This helper is name the Server Url helper, and it is used to return the URL or URI. Further modification or need for information should be done in the controller and then passed to the view for access.

When in the controller a developer may access methods of the HTTP request object directly. Every controller in Zend will give access to methods for finding parameters, domains, paths, and schemes of URI information. This is where user interface elements such as bread crumbs may be easily created and passed to the view while still following the MVC principle. It may take more lines of code than the Server Url view helper, but controllers give developers access to more options.

The library files you work with likely will not extend the Zend Controller Action. Thus, you won't have direct access to the request object. The good news is that the front controller is a singleton, as we discussed earlier. This means that all a developer needs to do is create a new instance of the object and then access the HTTP request just like in the controller. While this is very helpful, it's recommended that developers not make their own singletons in this fashion.

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:



Không có nhận xét nào:

Đăng nhận xét