CakePHP 4 ActivityLogger pluginAsyraf 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.
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 the plugin in .../src/Application.php:
Create activity_logs Table
run migration command in the console:
bin/cake migrations migrate -p Elastic/ActivityLogger
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 :)