Helping Hands
Helping Hands

Reputation: 5396

What is the proper way of setting the page title in CakePHP 2.6?

I am new to Cakephp and searching for how to set page titles and meta keywords,desc. for best SEO management.

I have checked any tutorials and question , I would like to ask that is it necessary to set up title in controller page and then fetch in any page of VIEW/mypage?

I am trying to set direct title in my home page in a way like :

$this->set('title', 'My Page Title');

But this is not working , Is there any way to direct set title without connection with controller?

I am using Version 2.6.3 of CakePhp.

Upvotes: 2

Views: 1418

Answers (2)

Inigo Flores
Inigo Flores

Reputation: 4469

Since CakePHP 2.5, the right way of setting the title is as follows:

In your layout:

$this->fetch('title')

In you view:

$this->assign('title', 'My Page Title')

The same applies to the meta tags:

In your layout:

<? echo $this->Html->meta('keywords',$this->fetch('keywords'));?>
<? echo $this->Html->meta('description',$this->fetch('description'));?>

In you view:

<? $this->assign('keywords', 'My meta tags')?>
<? $this->assign('description', 'My description')?>

More on CakePHP Layouts.

Upvotes: 3

code-kobold
code-kobold

Reputation: 824

The title tag would be set in the default.ctp of the layout in use, e.g. app/View/Layouts/default.ctp. You can hardcode your values there.

The displayed value comes from the PagesController::display() (app/Controller/PagesController.php), generated from the directory the rendered view resides in.

Setting your SEO title in a controller $this->set('seotitel', 'Foo') should propagate the value to the default.ctp, where you can display it:

<title>
    <?php echo $seotitel ?>
</title>

N.B. this is untested due to lack of a running 2.6 instance.

Upvotes: 0

Related Questions