I want to create an API using laravel which will do the below jobs, this API will take…
- User
I want to create an API using laravel which will do the below jobs,
this API will take everything from request and save all its body and also the response body as a document to mongodb collection.
this API will also log that request and its response to a file. it would be perfect if we make a file everyday and compress it to a gz file to store all these request daily.
- Assistant
To create an API in Laravel that fulfills these requirements, follow these steps:
- Install Laravel and create a new project:
composer global require laravel/installer laravel new MyApi- Install the necessary packages:
- For MongoDB, use jenssegers/mongodb package:
composer require jenssegers/mongodb- For logging, Laravel comes with Monolog which supports Daily and Rotating Logs. No additional package is needed.
- Configure MongoDB:
- Add the MongoDB configuration in the
config/database.phpfile:
'mongodb' => [ 'driver' => 'mongodb', 'host' => env('MONGO_DB_HOST', 'localhost'), 'port' => env('MONGO_DB_PORT', 27017), 'database' => env('MONGO_DB_DATABASE', 'test'), 'username' => env('MONGO_DB_USERNAME', ''), 'password' => env('MONGO_DB_PASSWORD', ''), 'options' => [ 'database' => env('MONGO_DB_AUTHENTICATION_DATABASE', 'admin') ], ],- Add the environment variables in the
.envfile:
MONGO_DB_HOST=localhost MONGO_DB_PORT=27017 MONGO_DB_DATABASE=my_api MONGO_DB_USERNAME=mongo_user MONGO_DB_PASSWORD=mongo_pass MONGO_DB_AUTHENTICATION_DATABASE=admin- Create a model for the request and response documents in MongoDB:
- Create a new Eloquent model:
php artisan make:model ApiRequest- Update the
ApiRequestmodel (app/Models/ApiRequest.php) to extend the MongoDB's Eloquent model:
use Jenssegers\Mongodb\Eloquent\Model; class ApiRequest extends Model { protected $collection = 'api_requests'; // Define the MongoDB collection protected $guarded = []; // Allow mass assignment of all fields }- Create an API route and a corresponding controller:
- Create a new API controller:
php artisan make:controller ApiController- Put the test route in
routes/api.php:
Route::any('store-request-and-response', [App\Http\Controllers\ApiController::class, 'storeRequestAndResponse']);- Update the ApiController to store the request and response:
- In
app/Http/Controllers/ApiController.php, update the controller as follows:
use App\Models\ApiRequest; use Illuminate\Http\Request; public function storeRequestAndResponse(Request $request) { $requestBody = $request->getContent(); $responseBody = 'Your custom response logic'; // Save the request and response to MongoDB ApiRequest::create([ 'request_body' => $requestBody, 'response_body' => $responseBody ]); // Log the request and response Log::channel('daily')->info("Request: {$requestBody}, Response: {$responseBody}"); // Return the response return response()->json(['message' => $responseBody]); }- Configure daily log rotation:
- In
config/logging.php, Laravel should be already configured to use daily logs by default:
'daily' => [ 'driver' => 'daily', 'path' => storage_path('logs/laravel.log'), 'level' => env('LOG_LEVEL', 'debug'), 'days' => 14, ],By following these steps, you will create a simple API with Laravel that accepts requests, saves their body and their response body as documents in a MongoDB collection, and logs requests and their responses to a file which is rotated daily.
To run the API, use:
php artisan serveRemember to start the MongoDB service and create the necessary user and database before running the API.
You can use a scheduler to compress the daily logs into
.gzformat on a daily basis. For instance, in Unix-based operating systems, you can usecronwithgzipto achieve this.