在 Layouts/default.ctp 中,您将在第 39 行看到类似这样的内容:
<div class="header-title">
<span><?= $this->fetch('title') ?></span>
</div>
fetch
表明它是一个 View block 。我在任何地方都找不到这个 View block 。
目前只显示controller的大写复数形式。意思是说如果你在 /users/add
, fetch('title');
给你 'Users'
我想改变它。所以我尝试了以下方法:
$this->set('title', 'Login');
在 /users/login
Controller 操作中。
没用。
我也试过
$this->assign('title', 'Login');
在 /users/login
Controller 操作中。
我收到此错误消息:
Call to undefined method App\Controller\UsersController::assign()
我阅读了文档并来自 here
我明白了
Assigning a block’s content is often useful when you want to convert a view variable into a block. For example, you may want to use a block for the page title, and sometimes assign the title as a view variable in the controller:
强调我的。
这表明您可以在 Controller 内部使用assign
。我想我已经证明这是错误的。
也许文档中有错字。请告知如何设置标题
最佳答案
感谢 irc channel #cakephp 的 dakota,这是如何做到的。
在 UsersController.php
中:
$this->set('title', 'Login');
在 src/Template/Layouts/default.ctp
中
在 $this->fetch('title');
之上
写:
if (isset($title)) {
$this->assign('title', $title);
}
问题是cakephp 3如何设置默认值?
答案可在 https://github.com/cakephp/cakephp/blob/3.0/src/View/View.php#L468 中找到
附加图片以防链接失效
哪里可以看到会默认为 View 路径
关于cakephp - 如何更改 CakePHP 3 中 Controller 的 View block 内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26603377/