PHP code example of sonleu / barrier

1. Go to this page and download the library: Download sonleu/barrier library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sonleu / barrier example snippets


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

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
    }
}
terminal
php artisan make:barrier MyBarrier