如何在CakePHP 3中更改控制器中的视图块内容? [英] How to alter view blocks content from controller in CakePHP 3?
问题描述
Inside Layouts / default.ctp会在第39行看到类似的结果:
< div class = -title>
< span><?= $ this-> fetch('title')?>< / span>
< / div>
fetch
建议它是一个视图块。我找不到这个视图块在任何地方。
目前它只显示大写复数形式的控制器。这意味着如果你在 / users / add
, fetch('title');
c $ c>'Users'
我想更改它。所以我尝试以下:
$ this-> set('title','Login');控制器操作中的 / users / login
中的
。
我没有工作。
我也试过了
$ this-> assign('title','Login');控制器操作中的 / users / login
中的
。
我收到此错误消息:
调用未定义的方法App\Controller\UsersController :: assign()
我阅读文档和从此处
我得到
当你想将一个
视图变量转换为一个块时,分配一个块的内容通常是有用的。例如,您可能希望使用块
作为页面标题,有时会在控制器中将标题作为视图变量
分配:
强调我的。
这表明你可以使用 assign
内部控制器。我想我已经证明这是假的。
也许在文件中有一个错字。请告诉我如何设置标题
这是如何做到感谢irc频道#cakephp的dakota。 / p>
内部 UsersController.php
:
$ this-> set('title','Login');
内部 src / Template / Layouts / default.ctp $ c $在
$ this-> fetch('title');
$ b
写:
if(isset($ title)){
$ this - > assign('title',$ title);
}
问题是cakephp 3如何设置默认值?
回答位于 https://github.com/cakephp/cakephp/blob/3.0/src/View/php#L468
附加图片
>
可以看到它将默认为视图路径
Inside Layouts/default.ctp you will see something like this at line 39:
<div class="header-title">
<span><?= $this->fetch('title') ?></span>
</div>
fetch
suggests that it is a view block. I couldn't find this view block anywhere.
Currently it just displays the capitalized plural form of controller. Meaning to say if you are at /users/add
, the fetch('title');
gives you 'Users'
I want to change it. So I tried the following:
$this->set('title', 'Login');
in the /users/login
controller action.
Did not work.
I also tried
$this->assign('title', 'Login');
in the /users/login
controller action.
I get this error message:
Call to undefined method App\Controller\UsersController::assign()
I read the docs and from here
I get
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:
Emphasis mine.
This suggests that you can use assign
inside controller. I think I have proven that this is false.
Perhaps there is a typo in the documents. Please advise how I can set the title
This is how to do it thanks to dakota of the irc channel #cakephp.
Inside UsersController.php
:
$this->set('title', 'Login');
Inside src/Template/Layouts/default.ctp
above the $this->fetch('title');
write:
if (isset($title)) {
$this->assign('title', $title);
}
The question would be how does cakephp 3 set the default value?
Answer is found in https://github.com/cakephp/cakephp/blob/3.0/src/View/View.php#L468
Append image just in case link rot
Where you can see that it will be defaulted to the view path
这篇关于如何在CakePHP 3中更改控制器中的视图块内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!