Skip to content
CODE THE PIXEL
CakePHP

CakePHP 4 Audit Trail Using AuditStash Plugin

Asyraf Wahi Anuar Updated 3 min read

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.

This tutorial may be outdated

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

Tech stack

  • CakePHP 4.x

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. The audit log allows the web application to offer systematic tracking of modifications made to specific data. This is extremely beneficial for any online application that requires tracking of content and data.

Repository: https://github.com/lorenzo/audit-stash

Installation

You can install this plugin into your CakePHP application using composer and executing the following lines at the root of your application.

Terminal
composer require lorenzo/audit-stash

Load the plugin using the following command:

Terminal
bin/cake plugin load AuditStash

Configuration

In .../config/app.php file add the following configuration:

config/app.phpPHP
'AuditStash' => [
    'persister' => 'AuditStash\Persister\TablePersister'
]

Migration

Create Auditstash table using migrations:

Terminal
bin/cake migrations migrate -p AuditStash -t 20171018185609

Bake the auditlogs table:

Terminal
bin/cake bake model AuditLogs

Enable Audit Log

Enabling the Audit Log in any of your table classes is as simple as adding a behaviour in the initialize() function:

PHP
class ArticlesTable extends Table
{
    public function initialize(array $config = [])
    {
        ...
        $this->addBehavior('AuditStash.AuditLog');
    }
}

Configuring The Behavior

The AuditLog behaviour can be configured to ignore certain fields. By default it ignores the created and modified fields:

PHP
class ArticlesTable extends Table
{
    public function initialize(array $config = [])
    {
        ...
        $this->addBehavior('AuditStash.AuditLog', [
            'blacklist' => ['created', 'modified', 'another_field_name']
        ]);
    }
}

You also can define a whitelist. This means that only the fields listed in that array will be tracked by the behaviour:

PHP
public function initialize(array $config = [])
{
    ...
    $this->addBehavior('AuditStash.AuditLog', [
        'whitelist' => ['title', 'description', 'author_id']
    ]);
}

That's all. Happy coding :)

Did this tutorial help?

238 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 Audit Log Plugin

An audit trail (audit logs) is a chronological log meant to capture any changes made to the electronic contents. The logs are used to be evidence for any changes that affected the contents.

3 min read

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 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