Monday, 19 February 2007
Manage 404 Errors in the Zend Framework? |
| |
|
| |
Richard Lord in a new entry over his blog shows you how to manage 404 errors in the Zend Framework. He speaks about early versions of the Zend Framework that had a noRoute action that was called when the correct action couldn’t be found. This was a way to deal with some page not found errors but he says it does not exist anymore. Though he says, it is still possible to handle non-existent actions using the __call() method of the controller class. But there’s no obvious way to deal with all page not found errors in one place, including instances where the controller doesn’t exist, he says.
To explain the 404 errors, he explains how the Zend Framework works. He says, the Zend Framework is based around controllers and actions, using URL’s of the form http://www.example.com/controller/action. If no action is specified the index action is used, and if no controller is specified the index controller is used. You can modify the mapping of URLs to controllers and actions by setting up different rewrite routers in the front controller, he explains.
He says, a 404 error should occur when the controller or action specified in the URL is not defined. But instead of a 404 error, the Zend Framework throws an exception because the controller class or the action method can’t be found. He explains that to generate an appropriate 404 error with a custom page you need either to intercept the request before the error occurs or to catch the exception after the error occurs, and in either case to redirect to an appropriate controller and action.
Richard creates a controller plug-in to do the above function. In the pre-dispatch method it checks the controller class if it can be loaded and whether it contains the required action. If both are absent, it redirects the request to the index action of the noroute controller. Then one just creates an appropriate noroute controller to display the page not found error. He gives a plug-in code to carry out the process.
After the code is laid out, he says to register this plug-in with the front controller and set-up the NorouteController class to display the error page. He also says to send a 404 error header from the noroute controller.
Richard warns that the plug-in doesn’t deal with modules—mainly because he didn’t use modules to explain the process and couldn’t decide whether the NorouteController class should be global or per-module. But, however he adds that it shouldn’t be too hard to add module handling to the code.
|
| |
|
Read the Post
|
| |
|
|
| |
|
|
| |
|