avolle / cakephp-title
A title plugin for CakePHP
2.0.0
2023-10-14 18:46 UTC
Requires
- php: ^8.1
- cakephp/cakephp: ^5.0.0
Requires (Dev)
- cakephp/cakephp-codesniffer: ^5.0
- phpstan/phpstan: ^1.10.33
- phpunit/phpunit: ^10.1.0
README
A plugin to automatically generate titles for web pages using routes from the HTTP request url.
This plugin will automatically generate a working title using values such as the request's controller
, action
, prefix
if there is one and the app name
if you provide one.
You can set our own format by configuring the Component when loading it in your application.
Version map:
Configuration options:
format
- How the generated title should be formatted. See valid placeholders below.- Default format:
{{prefix} - }{{controller} - }{{action} - }{{displayField}}{ » {appName}}
- Example generated: Admin - Files Types - View - PDF ยป CakePHP application
- Default format:
appName
- Your application's name. Used as replacement for theappName
placeholder.ignoreIndex
- If true, and the requested action isindex
, then the action will not be placed in the titleshowDisplayFieldOnView
- If true, the display field value (e.g. the files type's name) will be placed in the title.
Valid placeholders:
appName
- Will be replaced by the app's name, if you provide one in the component's configurationcontroller
- Will be replaced by the requested controlleraction
- Will be replaced by the requested actionprefix
- Will be replaced by the requested prefix, if there is one.displayField
- If your action stores an entity variable in the view, the component attempts to get the entity's display field value to display in the title- For example: If you have a table called Tools and the display field is the tool's name, it will place the tool's name in the title.
- The display field value is not fetched on its own from the database. It attempts to find a view var in which to get the value from.
- Please note that the entity variable must be named using cake's convention. A Tool entity variable must be
$tool
. A FilesType entity must be named$filesType
.
Usage:
In your Application.php file:
// On the top, with your other use statements
use Avolle\Title\Plugin as TitlePlugin;
/**
* Boostrap method. Load required plugins
*
* @return void
*/
public function bootstrap(): void
{
$this->addPlugin(TitlePlugin::class, ['autload' => true]);
}
In your AppController.php file:
/**
* Initialization hook method.
*
* @return void
* @throws \Exception
*/
public function initialize(): void
{
parent::initialize();
$this->loadComponent('Avolle/Title.Title', [
'appName' => 'Title App',
'format' => '{{prefix} - }{{controller} - }{{action} - }{{displayField}}{ » {appName}}',
'ignoreIndex' => false,
'showDisplayFieldOnView' => true,
]);
}
In your layout file
<head>
<title><?= $title; ?></title>
</head>