dev-master
9999999-devLumen Extension for SRP (Single Responsibility Principle)
MIT
The Requires
- php >=7.1.3
- laravel/lumen-framework 5.6.*
by Moeen Basra
laravel lumen photon
Lumen Extension for SRP (Single Responsibility Principle)
An Extension to Laravel with SRP Design Pattern., (*1)
install the package using the following command, (*2)
composer require moeen-basra/photon
Open the file public/index.php
, (*3)
Replace the following code, (*4)
$response = $kernel->handle( $request = Illuminate\Http\Request::capture() );
with, (*5)
$app->alias('request', \Photon\Foundation\Http\Request::class); $response = $kernel->handle( $request = \Photon\Foundation\Http\Request::capture() );
Open the class App\Http\Controllers\Controller
and replace the following code, (*6)
use Illuminate\Routing\Controller as BaseController;
, (*7)
with, (*8)
use Photon\Foundation\Controller as BaseController;
, (*9)
Finally you can extends the app\Exceptions\Handler
with the following class, (*10)
\Photon\Foundation\Exceptions\Handler\Handler
, (*11)
or use the following traits in your existing exception handler, (*12)
use Photon\Foundation\Traits\MarshalTrait; use Photon\Foundation\Traits\JobDispatcherTrait;
and in the render method run the following job if request accepts application\json
, (*13)
$message = $exception->getMessage(); $class = get_class($exception); $code = $exception->getCode(); if ($request->expectsJson()) { return $this->run(JsonErrorResponseJob::class, [ 'message' => $message, 'code' => $class, 'status' => ($code < 100 || $code >= 600) ? 400 : $code, ]); }
Now you can create the following directories in the app
folder., (*14)
|-- app | |-- Domains | |-- Features | |-- Operations
Here is a sample code for a controller serving the feature., (*15)
namespace App\Http\Controllers\Api\Auth; use Illuminate\Http\JsonResponse; use Photon\Foundation\Controller; use App\Features\Api\Auth\RegisterFeature; class AuthController extends Controller { public function __construct() { $this->middleware('auth:api', ['only' => 'me', 'logout', 'refresh']); } /** * Register user * * @return JsonResponse */ public function register(): JsonResponse { return $this->serve(RegisterFeature::class); } }
Here is sample code for Feature running the job, (*16)
namespace App\Features\Api\Auth; use Photon\Foundation\Feature; use App\Operations\Auth\RegisterOperation; use Photon\Domains\Http\Jobs\JsonResponseJob; use App\Domains\Auth\Jobs\Register\ValidateRegisterRequestJob; class RegisterFeature extends Feature { public function handle() { $input = $this->run(ValidateRegisterRequestJob::class); $data = $this->run(RegisterOperation::class, compact('input')); return $this->run(new JsonResponseJob($data)); } }
Lumen Extension for SRP (Single Responsibility Principle)
MIT
laravel lumen photon