在 CakePHP 2.6 中设置页面标题的正确方法是什么?

What is the proper way of setting the page title in CakePHP 2.6?

我是 Cakephp 的新手,正在搜索如何设置页面标题和元关键字,desc。最佳 SEO 管理。

教程和问题都查过了,请问是否需要在controller page中设置title,然后在VIEW/mypage的任意页面中fetch?

我正在尝试在我的主页中设置直接标题,方式如下:

$this->set('title', 'My Page Title');

但是这不行,有什么办法可以不连接控制器直接设置标题吗?

我使用的是 CakePhp 2.6.3 版本。

标题标签将设置在正在使用的布局的 default.ctp 中,例如app/View/Layouts/default.ctp。您可以在那里硬编码您的值。

显示的值来自 PagesController::display() (app/Controller/PagesController.php),从渲染视图所在的目录生成。

在控制器 $this->set('seotitel', 'Foo') 中设置您的 SEO 标题应该将值传播到 default.ctp,您可以在其中显示它:

<title>
    <?php echo $seotitel ?>
</title>

N.B。由于缺少 运行 2.6 实例,因此未经测试。

从 CakePHP 2.5 开始,title 的正确设置方法如下:

在您的布局中:

$this->fetch('title')

在您看来:

$this->assign('title', 'My Page Title')

同样适用于 元标签:

在您的布局中:

<? echo $this->Html->meta('keywords',$this->fetch('keywords'));?>
<? echo $this->Html->meta('description',$this->fetch('description'));?>

在您看来:

<? $this->assign('keywords', 'My meta tags')?>
<? $this->assign('description', 'My description')?>

更多关于 CakePHP Layouts