404 Custom Page in CakePHP 3.X

I want to create one 404 custom page for all errors coming to the website for the workbench. For example, if I get a missing controller or a viewing error, it will be redirected to. http://example.com/404.html,Also, in some cases, I intentionally redirect ithttp://example.com/404.html

Earlier in CakePHP 2.x this was done by adding the following action in AppContoller.php

public function appError($error) {
    $this->redirect('/page-not-found.html',301,false);
} 

But it does not work in CakePHP 3.x, I want to reproduce the same behavior in CakePHP 3.x

+4
source share
3 answers

Do not redirect

The right action to take if a page should display 404 is to display 404.

, , 301 , URL . , , , 404.html 200 ( -), 404, .

, , :

public function view($id)
{
    $article = $this->Articles->get($id);
    $this->set(compact('article'));
}

get :

get , Cake\Datasource\Exception\RecordNotFoundException. , CakePHP 404.

", ", , , 404.

404

404 500

4xx 5xx error400.ctp error500.ctp .

, , .

+2

get , a Cake\Datasource\Exception\RecordNotFoundException. , CakePHP 404.

0

The easiest way can be simply implemented in two stages,

Step 1:

disable debug mode in config / app.php

Step 2:

Replace the content src/Template/Layout/error.ctpwith your own layout.

0
source

Source: https://habr.com/ru/post/1606130/


All Articles