PHP code example of michaeljennings / route-guards
1. Go to this page and download the library: Download michaeljennings/route-guards 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/ */
use Illuminate\Routing\Route;
use MichaelJennings\RouteGuards\RouteGuard;
class ProductGuard extends RouteGuard
{
public function authorize(Route $route): bool
{
return Auth::user()->hasPermission('products.read');
}
}
use Illuminate\Routing\Route;
use MichaelJennings\RouteGuards\RouteGuard;
class ProductGuard extends RouteGuard
{
public function index(Route $route): bool
{
return Auth::user()->hasPermission('products.read');
}
public function store(Route $route): bool
{
return Auth::user()->hasPermission('products.create');
}
}
use Illuminate\Routing\Route;
use MichaelJennings\RouteGuards\RouteGuard;
class ProductGuard extends RouteGuard
{
// This works for authorize
public function authorize(Route $route, Model $model): bool
{
// Check the user can access the model
}
// It also works for the custom methods
public function update(Route $route, Model $model): bool
{
// Check the user can access the model
}
}
use Illuminate\Routing\Route;
use MichaelJennings\RouteGuards\RouteGuard;
class ProductGuard extends RouteGuard
{
public function __construct(ProductRepository $productRepository) {
$this->productRepository = $productRepository;
}
// This works for authorize
public function authorize(Route $route, Model $model): bool
{
// Check the user can access the model
}
protected function find(Route $route, string $binding)
{
return $this->productRepository->find(
$route->parameter($binding)
);
}
}
use Illuminate\Routing\Route;
use MichaelJennings\RouteGuards\RouteGuard;
class ProductGuard extends RouteGuard
{
public function authorize(Route $route): bool
{
return false;
}
protected function authorizationFailed(): void
{
throw new CustomException();
}
}
use Illuminate\Routing\Route;
use MichaelJennings\RouteGuards\RouteGuard;
class ProductGuard extends RouteGuard
{
public function index(Route $route): bool
{
return false;
}
protected function indexFailed(): void
{
throw new IndexException();
}
public function create(Route $route): bool
{
return false;
}
protected function createFailed(): void
{
throw new CreateException();
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.