Using Zend Framework To Get HTTP Information

|

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.

There are few scenarios where using a singleton design pattern is deemed acceptable. Zend Framework uses it as a pattern for the front controller, and it works great for accessing request information from anywhere in the application. The reason most developers don't use this pattern is because it may be abused, so be careful not to call the front controller where it shouldn't be. A good example is with using logic code in the view: this goes against MVC principle.

When working in the view the developer is limited to what he or she may do. This is because the view is not supposed to have any logic code in it. To remedy this, Zend Framework developers created the Server Url view helper. It returns the URI or URL to build a base URL path. When further elements from the request object are needed, developers should instead put logic in the controller and access the data by passing it to the view.

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.

When working in library files you do not have direct access to the Zend Controller Action class. Thus, you can't get direct access to the HTTP request object. Instead you can create an instance of the front controller and access the information just like you would in the controller as previously discussed. This is one of the few times you can access information in this style: singleton patterns are used few and far between.

The base URL helper is another option for those who don't want to build the root path every time they need to build a link. The base URL helper is set in the application configuration file or in the bootstrap. There are several implementations of this functionality, so use what best works for your application. This is as close to a singleton object as you should get to making your own global helper file.

Final Thoughts

Zend Framework gives easy access to URI information, just like PHP does. The difference is that Zend Framework makes the process easier and does so according to MVC design. The best part is that you can depend on the methods to return what you need - you won't have to build your own methods and functions to get URL variables.




About the Author:



0 comments:

Post a Comment

 

©2009 Minh Vu's Blog | Template Blue by TNB