</> Code The Pixel

CakePHP 4 ActivityLogger plugin

Asyraf Wahi Anuar - November 30, 2021

Estimated reading time: 1 minute, 2 seconds

Activity logging is one of the very useful features in web applications since it can provide a track of activities in the web content. CakePHP Activity Logger Plugin enables the developer or administrator to log the activities and changes to the web content. This tutorial will show how to integrate the CakePHP Activity Logger Plugin into your web application. 

Repository: https://github.com/elstc/cakephp-activity-logger

Installation
Currently, when this tutorial is written, the v2.0.0-rc5 is the latest compatible version with CakePHP 4. Use the following command to download:

composer require elstc/cakephp-activity-logger:v2.0.0-rc5

or if a stable version has been released for CakePHP 4, use the following command:

composer require elstc/cakephp-activity-logger


Load plugin
Load the plugin in .../src/Application.php:

$this->addPlugin('Elastic/ActivityLogger');


Create activity_logs Table
run migration command in the console:

bin/cake migrations migrate -p Elastic/ActivityLogger


Log Behaviour
Add the following code to your table eg in .../src/Model/Table/ArticlesTable.php

$this->addBehavior('Elastic/ActivityLogger.Logger', [
    'scope' => [
        //'Users',
        //'Authors',
    ],
    'systemScope' => false,
]);


At this point, it will log create, update and delete action. If you need to set the log message, add the following code before save method.

$this->Articles->setLogMessage('Transfer to Archived', true);


That's all. Happy coding :)


Cite this article (APA 6th Edition)

Popular
CakePHP 4 Print PDF Using CakePDF
May, 17 2020
CakePHP 4 Authentication Using Auth...
May, 14 2020
CakePHP 4 Sending Email
February, 01 2022
CakePHP 4 jQuery Date Time Picker
October, 01 2018
CakePHP 4 Export To CSV
May, 29 2020
CakePHP 4 Authentication Using...
May, 11 2020
CakePHP 4 Find, Sort & Count
June, 02 2020
Share
Sharing Link
Click the icon to share