nekoos/lumen-response-trick

Replace the lumen response facade with configurable interface

1.0.0 2019-09-05 16:04 UTC

This package is auto-updated.

Last update: 2024-11-06 03:18:56 UTC


README

This library presents an alternative for replacing the default classes used by Lumen

  • Illuminate\Http\Response
  • Illuminate\Http\JsonResponse;
  • Symfony\Component\HttpFoundation\BinaryFileResponse;
  • Symfony\Component\HttpFoundation\StreamedResponse;

Installation

composer require nekoos/lumen-response-trick

Usage

Basic use

Suppose you have created a custom class for common responses

use Illuminate\Http\Response;

class MyOverrideResponse extends Response { ... }

and one for the json answers

use Illuminate\Http\JsonResponse;

class MyOverrideJsonResponse extends Response { ... }

Now you can add these lines to your initial load file

# path: bootstrap/app

use NekoOs\Override\Laravel\Lumen\Http\ResponseFactory;

ResponseFactory::use(MyOverrideResponse::class);
ResponseFactory::use(MyOverrideJsonResponse::class);

This are expected results

response('common messaje')              # return instance of MyOverrideResponse
response()->json('common messaje')      # return instance of MyOverrideJsonResponse

Customized use

You could even use response instances according to specific conditions

# path: bootstrap/app

use NekoOs\Override\Laravel\Lumen\Http\ResponseFactory;

ResponseFactory::use(function (...$arguments) { ... }, Illuminate\Http\Response);

or even register a service provider

# path: app/Providers/MyResponseServiceProvider.php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Laravel\Lumen\Http\ResponseFactory;
use MyOverrideResponse;

class MyResponseServiceProvider extends ServiceProvider
{
    /**
     * register()
     */
    public function register()
    {
        $view = $this->app->make('view');
        $this->app->singleton(ResponseFactory::class, function () use ($view) {
            return new MyOverrideResponse($view);
        });
    }
}