Skip to content
CODE THE PIXEL
CakePHP

CakePHP 4 Slugged Behavior

Asyraf Wahi Anuar Updated 4 min read

Slug is the part of the URL that identifies a particular page on the web or system in an easy reading form. Slug is generated based on a specific field such as article title, name, event or any relevant field.

This tutorial may be outdated

It was last reviewed 4 years ago. Some steps may differ in newer versions.

Tech stack

  • CakePHP 4.x

Slug is the part of the URL that identifies a particular page on the web or system in an easy reading form. Slug is generated based on a specific field such as article title, name, event or any relevant field. If you have a document title (CakePHP Note 2020) and you upload it into your system the uploaded document will get its own unique ID which will be used as a reference ID to access the document. A standard URL to access the uploaded document will be eg: localhost/myCake/documents/11 which 11 is an ID (auto-generated) for the uploaded document (CakePHP Note 2020). Let say that the documents table have (id, title and year), so the slug can be generated based on the title of the documents which will generate the URL as localhost/myCake/documents/cakephp-note-2020. Use the CakePHP Tools Plugin to easily generate the slug.

Download the CakePHP Tools Plugin

Download CakePDF plugin via composer:

Terminal
composer require dereuromark/cakephp-tools

Load the plugin using the console:

Terminal
bin/cake plugin load Tools

or manually add the following code to ...\src\Application.php

src/Application.phpPHP
$this->addPlugin('Tools');

Configuration

Make sure your database table has 'slug' column eg table documents [id, title, year, slug]. If you need to store the slug in the different column name, use 'slug' => 'columnName' in the configuration.

Attach the following codes to your model in initialize() section:

PHP
$this->addBehavior('Tools.Slugged');

Then add the following configuration codes to set the slug label:

PHP
$this->addBehavior('Tools.Slugged',
    ['label' => 'title', 'unique' => true, 'mode' => 'ascii']
);

You can change the 'label' => 'title' to any field that is suitable for your requirement. The slugged will be automatically stored into your database field eg name input: CakePHP Note 2020, so it will be processed and stored as cakephp-note-2020. This slug normally will remain as the original even you edited your title to preserve the access link. However, you can overwrite the slug if it is necessary by adding the following checkbox into your edit page:

PHP
echo $this->Form->field('overwrite_slug', ['type' => 'checkbox']); 

To access your record based on slug instead of the ID, the controller should look like these:

PHP
public function view($slug)
{
    $document = $this->Documents
        ->findBySlug($slug)
        ->firstOrFail();
    $this->set(compact('document'));
}

The view button should navigate based on slug as shown below:

PHP
<?= $this->Html->link(__('View'), ['action' => 'view', $user->slug], ['class' => 'btn btn-primary', 'escape' => false]) ?>

Now you should be able to access the records based on the slugs. It also makes the URL more readable and easy.
That all. Happy coding :)

Did this tutorial help?

207 readers found this helpful

Comments

No comments yet. Questions about this tutorial are welcome here: the answer helps the next reader too.

Leave a comment

Markdown works: `code`, ``` blocks, **bold**, links.

First comments are reviewed before they appear.

CakePHP

CakePHP 4 Soft Delete

A soft delete marks a record as no longer active or valid without deleting it from the database. Soft deletion is a widely used pattern applied for business applications.

5 min read

CakePHP

CakePHP 4 Audit Trail Using AuditStash Plugin

This plugin adds an "audit trail" to any of your Table classes in your application, allowing the web applications to record the creation, change, or deletion of any table's entities. In theory, it will store the old data before replacing it with the new data or content.

3 min read

CakePHP

CakePHP 4 HTML Minify Plugin

Minify HTML source code for CakePHP 4. Minification enhances site performance and accessibility significantly, which immediately translates into a better user experience.

2 min read