1. Go to this page and download the library: Download muan/laravel-acl 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 Muan\Acl\Traits\{HasRolesTrait, HasPermissionsTrait};
class User extends Authenticatable
{
use HasRolesTrait, HasPermissionsTrait;
// ... Your User Model Code
}
class User extends Authenticatable
{
// ...
/**
* Attach base role
*/
public $baseRole = 'user';
// ...
}
if ($user->hasRole('admin')) {
// User is admin
}
// or
if ($user->hasRole('admin', 'writer')) {
// User is admin or writer
}
if ($user->hasPermission('create post')) {
// User has permission "create post"
}
$user->attachPermission("update post");
$user->detachPermission("remove post");
$user->clearPermissions();
Route::middleware(['role:admin'])->group(function() {
// Only for user with role admin
Route::get('/admin', function() {
// some code
});
});
Route::middleware(['permission:create post'])->group(function() {
// Only for user with permission create post
Route::get('/admin/post', function() {
// some code
});
});
Route::middleware(['role:moderator', 'permission:remove post'])->group(function() {
// Only for user with role moderator and with permission create post
Route::get('/admin/post/remove', function() {
// some code
});
});