CakePHP-3.0: How to Set Page Title to Pages

Setting a page title is as simple as you think.

Suppose you have a few posts in your blog post. By default cakephp will fetch it's table name in the title. We assume you have ArticlesTable. According to the Cake convention you have an ArticlesController. You made a view function in the controller from where you want to display your posts dynamically.

Assuming you have:

  1. $article->title = “First Title of an Article”;
  2. $article->title = “Second Title of an Article”;

So whenever a visitor view title 1, the visitor will view the “First Title of an Article” in the browser title and when viewer visit title 2 will set “Second Title of an Article” dynamically.

In the controller, you have a variable in the view function is $article, which you set by $this->set(compact('article')); will be accessible from view or layout

In the src/Template/Articles/view.ctp, or view function of ArticleControllerjust add the $article->title to your title block like

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

This will make your title changed dynamically whenever visitors view your blog posts.

You can also set this settings globally from the layout. In the layout, just add the code above the $this->fetch('title'); like:

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

No Comments

Have you say: