drewlabs/laravel-http

Provides bridge to laravel framework HTTP components

v0.3.0 2024-03-21 19:18 UTC

This package is auto-updated.

Last update: 2024-04-21 19:32:20 UTC


README

The drewlabs/http package provides utility classes and functions for unified HTTP response API and middleware classes for handling CORS.

Providers

By default providers are automatically registered when running Laravel application after composer finishes installing the package.

  • For Lumen appliation we must manually register the providers in the bootstrap/app.php:
// bootstrap/app.php
// ...
// Register the HttpService provider
$app->register(Drewlabs\Laravel\Http\HttpServiceProvider::class);
// ...

Cors Middleware

To use the cors middleware in your application add the following code to your kernel based on the framework being used:

  • Laravel
    // app/Http/Kernel.php

    // ...
    protected $middleware = [
        // ...
        \Drewlabs\Laravel\Http\Middleware\Cors::class,
    ];
  • Lumen
    // bootstrap/app.php

    $app->middleware([
        // Other globally registered middlewares...
        \Drewlabs\Laravel\Http\Middleware\Cors::class,
    ]);
    // ...

Note In order to allow any host or method, or headers use the * in the matching key of the config array.

EmptyStringToNull Middleware

It's a midleware that convert all empty string query parameteres and empty request body entry to null.

  • Laravel

Note: Laravel already provide implementation for such case. But if you still want to use the current package middleware do it as follow.

    // app/Http/Kernel.php
    // ...
    protected $middleware = [
        // ...
        \Drewlabs\Laravel\Http\Middleware\EmptyStringToNull::class,
    ];
  • Lumen
    // bootstrap/app.php

    $app->middleware([
        // Other globally registered middlewares...
        \Drewlabs\Laravel\Http\Middleware\EmptyStringToNull::class,
    ]);
    // ...

Http package configuration

This configuration file contains middleware aliases keys definition for the application Http request handlers, like auth, policy middlewares.

  • Publishing the configuration files

php artisan vendor:publish --tag="drewlabs-http"