sonleu / barrier
This is a small package to handle pre-conditions in controllers
v1.0.0
2020-09-09 10:00 UTC
This package is auto-updated.
Last update: 2024-06-09 18:50:38 UTC
README
This package is used for handling pre-conditions before implementing any job in controller, along with built-in Middleware and Form Request of Laravel.
Table of Contents
Installation
Composer
Execute the following command to get the latest version of the package:
composer require sonleu/barrier
Usage
Generators
Create your barriers easily through the generator.
Commands
php artisan make:barrier MyBarrier
This will create a Barrier file inside App/Barriers folder.
Example
namespace App\Barriers; use SonLeu\Barrier\BarrierInterface; class FooBarrier implements BarrierInterface { protected $argument; // You can pass any argument here public function __construct ($argument) { $this->argument = $argument; } /** * @return bool */ public function passes(): bool { if (SOMETHING_SHOULD_NOT_BE_ALLOWED) { return false; } return true; } /** * @return string */ public function message(): string { return 'Your message if not pass'; } }
Use methods
You can use Barriers anywhere inside your controller or even in your business logic layers.
It should be put above your logic.
namespace App\Http\Controllers; use Illuminate\Http\Request; use SonLeu\Barrier\HasBarrier; use SonLeu\Barrier\Exceptions\BarrierNotPassedException; class FooController extends Controller { use HasBarrier; public function bar(Request $request) { try { $this->barrier([ new CheckIfTuesdayBarrier(), new CheckVirginityBarrier(auth()->user()), ]); } catch (BarrierNotPassedException $e) { return back()->with('error', $e->getMessage()); } //TODO: your logic } }