开发者

How do you set the page title on a custom 404 page?

I have a call to a 404 page in a controller:

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

It uses my custom开发者_运维知识库 404 page, but ignores the title. The title gets set to "Error".

How do you set it?

public.ctp (I'm not using blank)

<?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';
    }
}

action

$this->pageTitle = 'Page Not Found';
$this->cakeError('error404');


I just answered this question in another topic.

You'll have to create an error.php in your app's root with the following code:

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


Tested this, and it works:

In the AppController:

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

In the layout, blank.ctp, include:

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

In the action:

$this->cakeError('error404');


Try this then

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

The problem with your code is that errors default to the empty layout. If you aren't using that layout then you need to specify the error layout. In the example above RabidFire has you specify the blank layout. Specify instead the layout you wish to use - in this case public.ctp/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜