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.
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.
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 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.
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.
Closing Comments
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.
This Zend Framework tutorial was written by Chris Channing. See more of his development and thoughts on zend get current url parameters.
