PHP code example of yahyaerturan / audit-codeigniter4
1. Go to this page and download the library: Download yahyaerturan/audit-codeigniter4 library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
yahyaerturan / audit-codeigniter4 example snippets
namespace Config;
class Registrar
{
public static function Audit(): array
{
return [
'databaseGroup' => 'auditing',
'captureIpAddress' => true,
];
}
}
use YahyaErturan\Audit\Domain\Actor;
use YahyaErturan\Audit\Domain\AuditRecord;
use YahyaErturan\Audit\Domain\Change;
use YahyaErturan\Audit\Domain\Changes;
use YahyaErturan\Audit\Domain\Subject;
$entry = service('yahyaAuditRecorder')->record(
AuditRecord::for('invoice.paid')
->by(Actor::identified('user', '42', 'Ada Lovelace'))
->on(Subject::identified('invoice', 'INV-123', 'March invoice'))
->withChanges(Changes::from([
'status' => Change::replaced('open', 'paid'),
'paid_at' => Change::added('2026-09-06T12:00:00+00:00'),
]))
->withMetadata(['channel' => 'stripe', 'amount_cents' => 5000])
);
$id = $entry->id()->toString(); // canonical UUID string
use YahyaErturan\Audit\Domain\AuditId;
use YahyaErturan\Audit\Query\AuditQuery;
$reader = service('yahyaAuditReader');
// By exact id
$entry = $reader->find(AuditId::fromString('01890b1e-7c3a-7a10-9f3d-2b6f0a5c1d42'));
// Paged search — keyset pagination, canonical order occurred_at DESC, id DESC
$page = $reader->search(
AuditQuery::all()
->forTenant('acme')
->forEvent('invoice.paid')
->byActor('user', '42')
->limit(50)
);
foreach ($page->entries() as $row) {
// $row is an AuditEntry
}
if ($page->hasMore()) {
$next = $reader->search(
AuditQuery::all()->forTenant('acme')->after($page->nextCursor())
);
}
$db = db_connect(); // same group as Config\Audit::$databaseGroup ('default')
$db->transStart();
$invoices->markPaid($invoiceId); // your business mutation
service('yahyaAuditRecorder')->record( // the audit fact
\YahyaErturan\Audit\Domain\AuditRecord::for('invoice.paid')
->on(\YahyaErturan\Audit\Domain\Subject::identified('invoice', (string) $invoiceId))
);
$db->transComplete();
// The audit row exists if and only if the business mutation committed.
namespace Config;
use CodeIgniter\Config\BaseService;
use YahyaErturan\Audit\CodeIgniter4\ActorResolver;
class Services extends BaseService
{
public static function yahyaAuditActorResolver(bool $getShared = true): ActorResolver
{
return new \App\Audit\ShieldActorResolver(/* inject whatever you need */);
}
}
// app/Config/Filters.php
public array $globals = [
'before' => ['audit-context'],
];
// or per route group
$routes->group('api', ['filter' => 'audit-context'], static function ($routes) {
// ...
});
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.