cakephp - 如何更改 CakePHP 3 中 Controller 的 View block 内容?

标签 cakephp cakephp-3.0

在 Layouts/default.ctp 中,您将在第 39 行看到类似这样的内容:

        <div class="header-title">
            <span><?= $this->fetch('title') ?></span>
        </div>

fetch 表明它是一个 View block 。我在任何地方都找不到这个 View block 。

目前只显示controller的大写复数形式。意思是说如果你在 /users/addfetch('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 中找到

附加图片以防链接失效

enter image description here

哪里可以看到会默认为 View 路径

关于cakephp - 如何更改 CakePHP 3 中 Controller 的 View block 内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26603377/

相关文章:

mysql - Cakephp 3.x 自定义查询分页

php - CakePHP 上的计划任务

php - cakephp 3.0 如何使用值而不是 id 填充选择字段

mysql - 带有 "Between dates"的 CakePHP 模型

CAKEPHP 3 : Select * and sum() in one statement

php - CakePHP 3.1 不会保存简单的 hasMany 关系

php - 如何在 cakephp 2.2 中使用 $_SESSION 变量

Cakephp 3 全文搜索

mysql - Cakephp 3 - 带限制的缓存查询

php - CakePHP PDO 准备语句