Hello I would like to know if someone has any code snippet for customizing 404 not found and error pages ?
Maybe I misunderstood something, but you can modify the existing view scripts:
Do you mean this?
Yes. But I would like to create another view for error and 404 not found. And perhaps on bootstrap, catch these errors and show the appropriate view.
You can add on Module.php something like this code:
public function onDispatchError(MvcEvent $event)
{
$response = $event->getResponse();
if ($response->getStatusCode() == 404) {
$event->getViewModel()->setTemplate("layout/404");
}
}
In the view-file you can create a custom design
This is not necessary as it is already set in the configuration:
Do you want multiple designs for 404 or other errors?
Oh, I thought he meant an extra layout for the 404-Page.
hello, i am interesting in how to change default to the different 404-page per some action/module or other edge case?
thanks!
It’s been a long time, unfortunately, but perhaps by overwriting the controller plugin CreateHttpNotFoundModel. There you can set the template on view model.
Caution
The laminas-mvc components are considered feature-complete and are in security-only maintenance mode until the security support for PHP 8.4 ends, which will be 31st December 2028.
The Mezzio and Laminas Components are still in active development.