PHP code example of codeflextech / permission-manager

1. Go to this page and download the library: Download codeflextech/permission-manager 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/ */

    

codeflextech / permission-manager example snippets


use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable
{
    use HasRoles;
}

return [
    // URL prefix — default: /permission-manager
    'route_prefix' => 'permission-manager',

    // Middleware on all routes
    'middleware' => ['web', 'auth'],

    // Spatie guard
    'guard' => 'web',

    // Role name that bypasses permission checks
    'super_admin_role' => 'super_admin',

    // Your User model
    'user_model' => \App\Models\User::class,

    // Set to your app layout component to embed inside your app UI
    // e.g. 'layouts.app' — leave null to use package's standalone layout
    'layout' => null,

    // Rows per page
    'per_page' => 15,

    // Disable permission creation via UI (seed-only mode)
    'allow_create_permissions' => true,
];

'middleware' => ['web', 'auth', 'role:super_admin'],

'middleware' => ['web', 'auth', 'can:manage-permissions'],

// Check permission
$user->can('clients.create');
@can('clients.create') ... @endcan

// Check role
$user->hasRole('admin');
@role('admin') ... @endrole

// Assign role
$user->assignRole('admin');

// Sync roles
$user->syncRoles(['admin', 'editor']);

// Sync permissions to role
$role->syncPermissions(['clients.view', 'clients.create']);

class PermissionSeeder extends Seeder
{
    public function run(): void
    {
        $permissions = [
            // Clients
            'clients.view', 'clients.create', 'clients.edit', 'clients.delete',
            // Invoices
            'invoices.view', 'invoices.create', 'invoices.edit', 'invoices.delete',
            // Reports
            'reports.sales', 'reports.payments', 'reports.due',
            // Settings
            'roles.manage', 'permissions.manage', 'organizations.edit',
        ];

        foreach ($permissions as $permission) {
            Permission::firstOrCreate(['name' => $permission, 'guard_name' => 'web']);
        }

        // Create super admin role with all permissions
        $superAdmin = Role::firstOrCreate(['name' => 'super_admin']);
        $superAdmin->syncPermissions(Permission::all());
    }
}
bash
php artisan migrate
bash
php artisan vendor:publish --tag=permission-manager-views