How to set page title on page 404?

I have a 404 page call in the controller:

$this->set('title','Page Not Found');
$this->cakeError('error404');

It uses my own 404 page, but ignores the title. The header is set to Error.

How do you install it?

public.ctp (I do not use empty)

<?php header('Content-type: text/html; charset=UTF-8') ;?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <?php echo $html->charset('utf-8'); ?>
    <title><?php echo $title_for_layout?></title>

app_controller.php

function beforeRender() {
    if($this->name == 'CakeError') {
        $this->set('title_for_layout', 'Page Not Found');
        $this->layout = 'public';
    }
}

act

$this->pageTitle = 'Page Not Found';
$this->cakeError('error404');
+3
source share
3 answers

I just answered this question in another section .

You need to create error.phpin the root of your application using the following code:

<?php
class AppError extends ErrorHandler  {
    function error404($params) {
        $this->controller->set('title_for_layout', 'Your Title');
        parent::error404($params);
    }
}
+3
source

Tested and working:

In AppController :

function beforeRender(){
    if ($this->name == 'CakeError') {
        $this->layout = 'blank';
        $this->set('title_for_layout', 'Page Not Found');
    }
}

, blank.ctp, :

<title><?php echo $title_for_layout; ?> | Site Name</title>

:

$this->cakeError('error404');
0

,

function beforeRender(){
    if ($this->name == 'CakeError') {
        $this->layout = 'public';
        $this->set('title_for_layout', 'Page Not Found');
    }
}

The problem with your code is that the default errors are for a blank layout. If you are not using this layout, you need to specify the layout of the error. In the above example, RabidFire defines an empty layout. Indicate instead the layout you want to use - in this case public.ctp /

0
source

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


All Articles