Skip to content

A small component for managing page titles in CakePHP 5

Notifications You must be signed in to change notification settings

gutocf/page-title

Repository files navigation

PageTitleComponent

Build Status Coverage Status Latest Stable Version Total Downloads License PHPStan Packagist Version

Requirements

  • PHP 8.1+
  • CakePHP 5.0+

Note: For using with PHP 8.0 and CakePHP 4.3+ check out version 1.1.1

Installation

Install the plugin with composer

composer require gutocf/page-title

Plugin load

bin/cake plugin load PageTitle

Component load

Load the component in App\Controller\AppController:

$this->loadComponent('Gutocf/PageTitle.PageTitle', [
   'default' => 'MyApp Name', //Default page title - optional, default = null
   'var' => 'var_name_for_views', //Var name to set at view - optional, default = title
   'separator' => ' :: ', //Titles separator - optional, default = ' / '
   'reverseOrder' => true, //Display titles in reverse order of inclusion - optional, default = true
]);

You need to load the component in controllers or application's AppController (recomended).

Usage

To add titles to your page, simply call PageTitle::add method with one or more parameters:

$this->PageTitle->add('Articles', 'Add');

In Controller.beforeRender event, the component will set a variable with $config['var'] name for use in the views and templates, in this example Add :: Articles :: MyApp Name (Or MyApp Name :: Articles :: Add if reverseOrder option is false). You can set the page title by including this code in the template file src/templates/default.php

<head>
   <title><?= $this->get('var_name_for_views') ?></title>
</head>