Getting the current URL in a Zend Application is useful for when building templates and navigations. Luckily the designers of Zend Framework have made the process as simple as calling several different methods from your Zend 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.
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.
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.
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.
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.
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.
About the Author:
This Zend Framework tutorial was written by Chris Channing. See more of his development and thoughts on zend framework get url parameters.
No comments:
Post a Comment