简体   繁体   English

在cakePHP中,标题栏的默认值设置为控制器名称吗?

[英]default value of title block set to controller name in cakePHP?

In a cakephp layout I fetch a block in order to set the title of the page like so 在cakephp布局中,我获取一个块以便像这样设置页面标题

<title>Example - <?php echo $this->fetch('title');?></title>

I have noticed that unless I create a block 'title' in a view or assign some value to the block, the value of title always corresponds to the name of the controller. 我注意到,除非我在视图中创建一个块“ title”或为该块分配一些值,否则title的值始终与控制器的名称相对应。 I have not found this behaviour documented anywhere. 我没有发现任何地方记录此行为。 Is there any way to change this? 有什么办法可以改变吗? My cakePHP version is 2.7.5. 我的cakePHP版本是2.7.5。

You can pass a default value to be output by $this->fetch() if a value hasn't already been assigned using the second parameter:- 如果尚未使用第二个参数分配值,则可以传递默认值,以通过$this->fetch()输出:

echo $this->fetch('title', 'Default title');

To override the default just use $this->assign() in your view (before $this->fetch() is called):- 要覆盖默认值,只需在视图中使用$this->assign() (在调用$this->fetch()之前):-

$this->assign('title', 'Overridden title');

In your controller 在您的控制器中

class ExampleController extends AppController {

   // Set title in default for all this controller methods
   public $title = "Your title";

   // Override the title by setting title in method
   public function

        $this->set('title', 'Your title');
   }
}

In your layout or view 在您的布局或视图中

cakephp 2.x
<?php echo $this->fetch('title'); ?>

cakephp 3.x
<?= $this->fetch('title'); ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM