【问题标题】:How do you set the page title on a custom 404 page?如何在自定义 404 页面上设置页面标题?
【发布时间】:2010-11-28 19:35:41
【问题描述】:

我在控制器中调用了 404 页面:

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

它使用我的自定义 404 页面,但忽略了标题。标题设置为“错误”。

你是怎么设置的?

public.ctp(我没有使用空白)

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

动作

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

【问题讨论】:

    标签: php cakephp cakephp-1.2


    【解决方案1】:

    I just answered this question in another topic.

    您必须使用以下代码在应用的根目录中创建error.php

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

    【讨论】:

    • 嘿,我得到了这个工作!您代码中的错误是 title_for_layout 应该是标题,even 尽管我的布局确实在 TITLE 标签中使用了 $title_for_layout。如果您更新答案,我会将其标记为正确并投赞成票。谢谢!
    • 没有必要对所有内容都投反对票。这对我来说似乎很刻薄。至少我们正在努力。
    • 对不起!我没有刻意刻薄。你说得对,我不应该。它似乎并没有让我改变它。
    【解决方案2】:

    对此进行了测试,它可以工作:

    AppController中:

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

    布局的空白.ctp中,包括:

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

    动作中:

    $this->cakeError('error404');
    

    【讨论】:

    • 你能发布你的代码吗? (blank.ctp、AppController 和操作)
    【解决方案3】:

    那就试试这个

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

    您的代码的问题是错误默认为空布局。如果您不使用该布局,则需要指定错误布局。在上面的示例中,RabidFire 让您指定空白布局。指定您希望使用的布局 - 在本例中为 public.ctp/

    【讨论】:

    • 对不起,我应该清楚的。我确实将他的代码更新为正确的布局,以使用 public。正确的布局加载错误页面,所以我可以确认它正在工作。我只是无法设置页面标题。还有其他想法吗?
    • 我因帮助您做某事而感到沮丧?我们都没有发布错误的代码 - 我从字面上测试了我提供的内容并且它有效。只是说 - 这可能不是回答您未来问题的最佳方式。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-01
    • 2023-01-19
    相关资源
    最近更新 更多