Skip to content

boyvn2911/barrier

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

Laravel Barrier

This package is used for handling pre-conditions before implementing any job in controller, along with built-in Middleware and Form Request of Laravel.

Latest Stable Version Total Downloads Latest Unstable Version License

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
    }
}

About

Laravel package to handle pre-conditions

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages