0
votes

In the documentation for CakePHP 2.9.5 https://book.cakephp.org/2.0/en/views.html it says:

The $title_for_layout is deprecated as of 2.5, use $this->fetch('title') in your layout and $this->assign('title', 'page title') instead.

In app/View/Layouts/default.ctp I have this, which incidentally comes with CakePHP and has not been modified:

<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"

Where am I going wrong?

3
That's not what the default layouts title tag looks like, it looks like this: github.com/cakephp/cakephp/blob/2.9.5/app/View/Layouts/… - ndm
Either way, it doesn't fix the issue - Andy
I didn't mean to say that this would fix the issue, just that your statement that the file has not been modified, seems incorrect. The cited phrase is being defined in the $cakeDescription varibale in the default layout and error template, and nowhere else. So that, and the fact that the default template doesn't look like what you're showing, makes your question more unclear, and your problem less reproducable. - ndm
Ok sorry, I've edited it to show exactly what was in the file. The reason I summarised it was because I knew $cakeDescription was not causing the issue so was trying to make it more readable. Whether that line is there makes no difference however. - Andy
Is that still not working ? If you post whole code of default.ctp it will be more clear. - Manohar Khadka

3 Answers

2
votes

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:

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

The string for $cakeDescription is echo'd before the title set in your View. 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. 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.

0
votes

If that doesn't work for you - and 2.9.5 was a fun release try this:

in your View/Layouts/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'

so using the above code, the Title would be set at Prospector : companies in this instance.

You can of course capitalize as needed etc, dropping Prospector for your given preference.

full page below - note this is from a clean 2.9.5 install:

<?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.

-1
votes

in your controller

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

then in your view just

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