@gilbert
To create custom error pages in Phalcon, you need to follow these steps:
- Create a directory named “errors” in the views folder of your project.
- Inside the “errors” directory, create the following files:
404.volt: This file will be displayed when the requested page is not found.
500.volt: This file will be displayed when there is a server error.
- In your project’s bootstrap file, add the following code to register the view component:
$di->setShared('view', function () {
$view = new PhalconMvcView();
$view->setViewsDir('../app/views/');
return $view;
});
- Create a new class named “ErrorController” that extends the “PhalconMvcController” class. This controller will handle all errors in your project.
namespace MyAppControllers;
use PhalconMvcController;
class ErrorController extends Controller
{
public function show404Action()
{
$this->view->pick('errors/404');
}
public function show500Action()
{
$this->view->pick('errors/500');
}
}
- Add the following code to your project’s router to send error requests to the “ErrorController”:
$router->notFound(array(
'controller' => 'error',
'action' => 'show404'
));
$router->add('/error/show500', array(
'controller' => 'error',
'action' => 'show500'
));
- Finally, create a .htaccess file in your project’s public folder (if not already existing), and add the following code to redirect all error requests to the “ErrorController”:
ErrorDocument 404 /error/show404
ErrorDocument 500 /error/show500
That’s it! Now, whenever there is a 404 or 500 error, your custom error pages will be displayed.