This package is used for handling pre-conditions before implementing any job in controller, along with built-in Middleware and Form Request of Laravel.
Execute the following command to get the latest version of the package:
composer require sonleu/barrier
Create your barriers easily through the generator.
php artisan make:barrier MyBarrier
This will create a Barrier file inside App/Barriers folder.
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';
}
}
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
}
}