- How do I install Laravel Permission Manager in a Laravel 10+ project?
- Run `composer require hosseinhezami/laravel-permission-manager` and publish the migrations with `php artisan vendor:publish --provider="HosseinHezami\PermissionManager\PermissionManagerServiceProvider" --tag="migrations"`. Then run `php artisan migrate` to set up the database tables. The package uses Laravel’s auto-discovery, so no additional configuration is needed unless you’ve disabled it.
- Does this package support wildcard permissions like `admin.*` or `*admin`?
- Yes, the package supports wildcard permissions (e.g., `admin.*`, `*admin`, `admin*`). This allows flexible route-based permission checks, such as granting access to all routes under `admin` or matching partial route names. However, wildcards can impact performance with large permission sets, so test under your expected load.
- Can I use this package with multiple authentication guards (e.g., Sanctum, Session, API)?
- Yes, the package supports multi-guard authentication. Ensure the `PermissionTrait` is applied to all user models used with your guards. Test thoroughly across guards, as permissions must propagate correctly. The package works with Laravel’s built-in guards and third-party guard providers.
- How do I protect routes using middleware for roles or permissions?
- Use the provided middleware in your `routes/web.php` or `routes/api.php`. For example, `Route::middleware(['pm:role:admin'])->group(function () { ... })` or `Route::middleware(['pm:permission:posts.create'])->group(function () { ... })`. The package also includes a generic `pm` middleware for permission checks without specifying a role.
- Are there Blade directives for checking roles or permissions in views?
- Yes, the package includes `@hasRole('admin')` and `@hasPermission('posts.create')` directives for Blade templates. These directives simplify role-based UI rendering, such as hiding or showing elements based on user permissions. They work seamlessly with Laravel’s view layer.
- How do I cache permissions for better performance, and when should I invalidate the cache?
- Permissions are cached by default with a configurable duration in `config/permission-manager.php`. To invalidate the cache after role or permission updates, manually clear it with `PermissionManager::clearCache()` or implement event listeners (e.g., `RoleUpdated`) to trigger cache invalidation automatically.
- Does this package work with Laravel’s route caching (e.g., `php artisan route:cache`)?
- Yes, the package is compatible with Laravel 10+ and supports route caching. However, ensure your middleware and permission checks remain functional after caching. Test your application thoroughly after running `php artisan route:cache` to confirm no permission-related issues arise.
- Can I export or import roles and permissions for backup or deployment?
- Yes, the package includes Artisan commands for exporting and importing roles and permissions. Use `php artisan role:export` to generate a JSON file and `php artisan role:import` to restore roles and permissions. This is useful for deploying permission configurations across environments or backing up critical access control settings.
- What are the potential performance implications of using wildcard permissions?
- Wildcard permissions (e.g., `admin.*`) offer flexibility but can slow down permission checks if overused, especially with large permission sets. Benchmark performance under your expected load, particularly in high-traffic applications. Consider optimizing by using specific permissions where possible or implementing a permission hierarchy.
- How do I handle permission changes for existing users during development or deployment?
- Use the Artisan commands to manage roles and permissions safely. For example, `php artisan user:assign-role user_id role_name` assigns a role to a user. To avoid breaking workflows, test permission changes in a staging environment first. For critical applications, implement a rollback plan using the export/import commands or database backups.