Custom error page in CakePHP 4.X

In CakePHP 2.x I used this function in beforeRender() at AppController to use custom error page for all kind of error.

/if ($this->name == ‘CakeError’) { $this->layout = ‘front_end_error’; }

How to do this in cakePHP 4.x without changing Debug Level (false) ?


This seems like it should be handled (maybe even already?) by the error handler middleware, not in beforeRender.

Use ErrorController::beforeRender().

I added this

    public function beforeRender(EventInterface $event)

        $this->viewBuilder()->setLayout('error_404');  // layout

its not working when i try to enter wrong url like localhost/project/abcd

Now i am getting " Controller class Abcd could not be found.:clipboard:". error instead of error_404 page.

Test with debug turned off. In debug mode CakePHP will generate developer friendly error pages.