简体   繁体   English

CakePHP 2.9.5 $ this-> assign('title')不能按文档中所述运行

[英]CakePHP 2.9.5 $this->assign('title') does not work as described in documentation

In the documentation for CakePHP 2.9.5 https://book.cakephp.org/2.0/en/views.html it says: 在CakePHP 2.9.5的文档中https://book.cakephp.org/2.0/en/views.html中说:

The $title_for_layout is deprecated as of 2.5, use $this->fetch('title') in your layout and $this->assign('title', 'page title') instead. 从$ 2.5开始不推荐使用$ title_for_layout,在布局中使用$this->fetch('title') ,并使用$this->assign('title', 'page title')代替。

In app/View/Layouts/default.ctp I have this, which incidentally comes with CakePHP and has not been modified: 在app / View / Layouts / default.ctp中,我有此文件,它是CakePHP附带提供的,尚未修改:

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

And in my view file: 在我的视图文件中:

<?php $this->assign('title', 'My page title'); ?>

So this should output "My page title" in the <title> tag but just comes up with the default CakePHP title of "CakePHP: the rapid development php framework" 因此,这应该在<title>标记中输出“我的页面标题”,但只是带有默认的CakePHP标题“ CakePHP:快速开发php框架”

Where am I going wrong? 我要去哪里错了?

If that doesn't work for you - and 2.9.5 was a fun release try this: 如果这对您不起作用-2.9.5是一个有趣的发行版,请尝试以下操作:

in your View/Layouts/default.ctp 在您的视图/布局/ default.ctp中

parse the url and pull the path, set this after your title or as your title as your preference 解析网址并提取路径,在您的标题之后或作为您的首选项将其设置

snippet below: 下面的代码段:

<?php

$url = $_SERVER['REQUEST_URI'];
$url_array = parse_url($url);
preg_match('@/(?<path>[^/]+)@', $url_array['path'], $m);
$url_folder = $m['path'];

$cakeDescription = 'Prospector : '.$url_folder;
?>
<!DOCTYPE html>

<html>
<head>
    <?php echo $this->Html->charset(); ?>
    <title>
        <?php echo $cakeDescription ?>:
    </title>

The $url_folder contains the path I assume you are after, so if you view is example.com/companies/index for example the $url_folder contains 'companies' $ url_folder包含我假定的路径,因此,例如,如果您查看的是example.com/companies/index,则$ url_folder包含“ companies”

so using the above code, the Title would be set at Prospector : companies in this instance. 因此,使用上述代码,本例中的标题将设置为Prospector:公司。

You can of course capitalize as needed etc, dropping Prospector for your given preference. 当然,您可以根据需要使用大写形式,也可以根据自己的偏好删除Prospector。

full page below - note this is from a clean 2.9.5 install: 以下是整页-请注意,这是从全新的2.9.5安装开始的:

<?php

$url = $_SERVER['REQUEST_URI'];
$url_array = parse_url($url);
preg_match('@/(?<path>[^/]+)@', $url_array['path'], $m);
$url_folder = $m['path'];

$cakeDescription = 'Prospector : '.$url_folder;
?>
<!DOCTYPE html>
<html>
<head>
    <?php echo $this->Html->charset(); ?>
    <title>
        <?php echo $cakeDescription ?>:
    </title>
    <?php
        echo $this->Html->meta('icon');

        echo $this->Html->css('cake.generic');

        echo $this->fetch('meta');
        echo $this->fetch('css');
        echo $this->fetch('script');
    ?>
</head>
<body>
    <div id="container">
        <div id="header">
            <h1><?php echo $this->Html->link($cakeDescription, 'http://cakephp.org'); ?></h1>
        </div>
        <div id="content">

            <?php echo $this->Flash->render(); ?>

            <?php echo $this->fetch('content'); ?>
        </div>
        <div id="footer">
            <?php echo $this->Html->link(
                    $this->Html->image('cake.power.gif', array('alt' => $cakeDescription, 'border' => '0')),
                    'http://www.cakephp.org/',
                    array('target' => '_blank', 'escape' => false, 'id' => 'cake-powered')
                );
            ?>
            <p>

            </p>
        </div>
    </div>
    <?php echo $this->element('sql_dump'); ?>
</body>
</html>

Of course this would only work for you if you want the title to be the same as the model. 当然,仅当您希望标题与模型相同时,这才对您有用。

If you don't want the model name, but can use the model name to test against, then you could use $url_folder to test against in a switch. 如果您不想使用模型名称,但是可以使用模型名称进行测试,则可以在开关中使用$ url_folder进行测试。

The answer to this is that the original code I had works. 答案是我拥有的原始代码有效。 But there is a change required to the default layout file... 但是,默认布局文件需要更改...

In Layouts/default.ctp you have this on a clean installation: Layouts/default.ctp您可以进行全新安装:

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

The string for $cakeDescription is echo'd before the title set in your View. $cakeDescription的字符串在您的视图中设置的标题之前回显。 The default string is quite long and so if you're looking at it in a browser tab (as I was) it appears that the title never changes! 默认字符串很长,因此,如果您正在浏览器选项卡中查看它(就像我一样),标题似乎永远不会改变! Unless you view source. 除非您查看源代码。

In any case you need to remove this variable because otherwise all your titles will have Cake's default text prepended. 无论如何,您都需要删除此变量,因为否则所有标题都将带有Cake的默认文本。 So the solution is simply to change the above to: 因此,解决方案只是将以上内容更改为:

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

In your Views, the code works as documented: 在您的视图中,代码按文档所述工作:

$this->assign('title', 'Page title here'); 

This is a poor default on the Cake developers part as there's no reason you'd ever want their default text in the title tag in a real application. 对于Cake开发人员而言,这是一个很差的默认设置,因为您没有理由要在真实应用程序的title标签中使用其默认文本。

in your controller 在您的控制器中

$title = 'Title for your page';
$this->set(compact('title'));

then in your view just 然后在您看来

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

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

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