How to display a custom 404 error page
Overview
With symfony, you can easily define a custom error 404 page. You even have several ways to do it.
Introduction
The module and action to be called when a 404 error occurs is defined in the settings.yml configuration file of each application:
all:
.actions:
error_404_module: default
error_404_action: error404
The error404 action of the default module doesn't appear in the application directory, since it is part of symfony.
Solution 1 : change the configuration
You can specify a custom module and action in the settings.yml file:
default:
.actions:
error_404_module: errors
error_404_action: error404
Just initialize the new module with:
$ symfony init-module myapp errors
You need to create the action error404 in the file myproject/apps/myapp/modules/errors/actions/action.class.php, and the template error404Success.php in the directory myproject/apps/myapp/modules/errors/templates/. You now have full control over the content of your 404 error page.
Solution 2 : override the default template
A simpler way to do it is to override the default error404Success.php template. It must be located in the default module, so create it if it doesn't exist already.
$ cd myapp/modules
$ mkdir default
Symfony will first look for an action or a template here before calling the default ones. So create a file called error404Success.php in a newly created templates directory, write whatever you need into it, and you're done.
$ cd default
$ mkdir templates
$ cd templates
$ vi "error404Success.php"
Note: Why not use the symfony init-module here? Because it would create an actions.class.php file that would override all the actions of the default default module, including index, login, disabled, secure and unavailable. So using the symfony init-module command would imply erasing the newly created actions.class.php file to focus only on the template.
|