Own HTTP 4xx, 5xx errors

I know how to have own HTTP 4xx, 5xx errors by directly and manually editing certain Apache files and Apache configuration. But how to achieve the same inside Yii 2 Advanced Application?

Let’s say that instead of current error message, I want to display a 2-3 lines of HTML code and some image to the end user. I am thinking about something like these examples:

From which point (in Yii 2 Guide or code) to start? Where to store images that should be displayed when error appears? How to make sure that these images will be ready (published) when error occurs (which can occur at a very early stage of request processing).

Check the following section of the Guide
Handling Requests > Handling Errors > Customizing Error Display

Or just put your design in this file frontend/views/site/error.php

Quick and short solution,

Both solutions (thank you for them!) does not seems to be addressing my biggest concern.

My business requirement is to display text and some image. In order for this image to be displayed correctly I need to have some CSS, image asset and maybe even JS asset to be registered and available when error action is triggered (error view is displayed).

What will happen, If error occurs at the very early stage of request processing? For example at the action routing stage? If I am getting Yii 2 app lifecycle correctly, publishing registered frontend assets happens at the very, very end of request processing / request life-cycly.

Can I be 100% sure that my image (CSS, JS) is displayed / available / used correctly in any scenario? No matter at which lifecycle stage an error or exception appears?

As long as a view is used to display errors, all the assets registered for that specific view can be accessed.

1 Like