PHP code example of fazzinipierluigi / just-a-gate
1. Go to this page and download the library: Download fazzinipierluigi/just-a-gate 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/ */
fazzinipierluigi / just-a-gate example snippets
namespace App\Models;
use Fazzinipierluigi\JustAGate\Traits\Authorizable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Authorizable;
}
return [
/*
| Name used to register the ACL middleware.
| Apply it to routes with Route::middleware('acl').
| Type: string
| Default: 'acl'
*/
'middleware' => 'acl',
/*
| Extra permissions created during `permission:import`.
| Format: 'permission_key' => 'Display name'
| Type: array<string, string>
| Default: []
*/
'additional' => [
// 'reports.export' => 'Export reports',
],
/*
| When true, `permission:import` auto-generates one permission per existing role,
| with key "user.create.role_{slug}". Useful for controlling which roles a user
| is allowed to assign during registration/admin flows.
| Type: bool
| Default: true
*/
'role_user_creation' => true,
/*
| When true, `permission:import` deletes permissions that no longer correspond
| to any route protected by the 'acl' middleware or any 'additional' entry.
| Type: bool
| Default: true
*/
'clean_permission' => true,
/*
| Maps permission keys to human-readable names, applied during `permission:import`.
| Useful for overriding auto-generated names.
| Format: 'permission_key' => 'Display name'
| Type: array<string, string>
| Default: []
*/
'translate' => [
// 'user.index' => 'View user list',
],
/*
| Auto-assigns permissions to roles during `permission:import`.
| Format: 'permission_key' => ['role_slug_1', 'role_slug_2']
| Type: array<string, list<string>>
| Default: []
*/
'assign' => [
// 'user.index' => ['admin', 'moderator'],
],
];
use Fazzinipierluigi\JustAGate\Models\Permission;
// Direct model creation
$perm = Permission::create(['key' => 'post.index', 'name' => 'View posts']);
// Via Facade (recommended — also lowercases the key)
$perm = JustAGate::createPermission('post.index', 'View posts', 'Allows viewing the posts list');
public static function findByKey(mixed $key): ?Permission
namespace App\Livewire;
use Fazzinipierluigi\JustAGate\Attributes\RequiresPermission;
use Livewire\Component;
#[RequiresPermission('post.index')]
class PostsList extends Component
{
public function render()
{
return view('livewire.posts-list');
}
}
namespace App\Livewire;
use Fazzinipierluigi\JustAGate\Attributes\RequiresPermission;
use Livewire\Component;
class PostsList extends Component
{
public function render()
{
return view('livewire.posts-list');
}
#[RequiresPermission('post.destroy')]
public function delete(int $id): void
{
Post::findOrFail($id)->delete();
}
#[RequiresPermission('post.create')]
public function create(array $data): void
{
Post::create($data);
}
}
#[RequiresPermission('post.index')] // enforced on every request
class PostsList extends Component
{
#[RequiresPermission('post.destroy')] // additionally enforced on delete()
public function delete(int $id): void
{
Post::findOrFail($id)->delete();
}
}
// App\Livewire\PostsList.php
#[RequiresPermission('post.index')]
class PostsList extends Component { ... }
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD)]
class RequiresPermission
{
public function __construct(public readonly string $permission) {}
}