Access Log Middleware

After you require the package. When you do php artisan migrate you will notice a new migration/table getting migrated.

It is the create_access_logs_table migration.

That is because the AccessLogMiddleware saves logged api endpoints in this table.

Usage

To use the AccessLogMiddleware, all you have to do is wrap the middleware access.log around your desired endpoints like below

// api.php

Route::middleware(['access.log'])->group(function () {
    Route::get('/hello', function () {
        return 'Hello World';
    });
});

Result

After you wrapped the access.log middleware around your api endpoints.

Now, if you hit that endpoint yourUrl:8000/hello it will return the string Hello World.

If you go to your access log table now you will see that the endpoint information were saved to the access log database table.



Access Log Result Image



It has information like:

  1. The Ip Address
  2. The Method Type (Get, Post, Put, Delete, Options, and Etc...)
  3. The Endpoint Url
  4. The User Agent
  5. The Request Data (If it a Post/Put Request)
  6. The Timestamps (Created At, and Updated At)

Publish Models & Migrations

You can optionally publish the Models and the Migrations of this package If you want to customize anything:

Models:

You can publish the package's models through this command:

php artisan vendor:publish --tag=lara-util-x-models

Migrations

You can publish the package's migrations through this command:

php artisan vendor:publish --tag=lara-util-x-migrations