1. Go to this page and download the library: Download leoche/laravel-lpermissions 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 Leoche\LPermissions\Traits\HasRole;
class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
use Authenticatable, HasRole;
}
$role = new Role();
$role->name = 'Admin';
//The slug will be automatically generated from the role name
$role->save();
$user = User::find(1);
$user->setRole(2); // with id
//OR
$user->setRole("Admin"); // with slug/name
$user->removeRole();
$role = Role::find(1);
$role->setInheritRole(2); //with id
//OR
$role->setInheritRole("Admin");
$role->removeInheritRole();
$role = Role::find(1);
$role->setPermission("admin/*", "*");
$role->removePermission("/admin/*", "*");
$user = User::find(1);
$user->setPermission("secretpage", "GET");
$user->removePermission("secretpage", "GET");
$user = User::find(1);
$user->removeAllPermissions(); //delete all permissions of user
$user->getRole->removeAllPermissions(); //delete all permissions of user's role
$role = Role::find(1);
$role->removeAllPermissions();
Route::get('/home', function () {
return "You can go here";
});
...
Route::group(['middleware' => ['auth']], function () {
Route::get('/home1', function () {
return "You can go here if you're logged";
});
});
...
Route::group(['middleware' => ['permission']], function () {
Route::get('/home2', function () {
return "You can go here if you or your role have '/home2' or '/*' permission";
});
});
...
Route::group(['middleware' => ['auth','permission']], function () {
Route::get('/home3', function () {
return "You can go here if you're logged and you or your role have '/home3' or '/*' permission";
});
});
@permission('admin/dashboard')
//Only shown to users who can access to admin dashboard
@endpermission
...
@permission('admin/posts','post')
//Only shown to users who can access to admin posts with method POST
@endpermission
...
...
@role('moderator')
//Only shown to moderators role
@endrole
...
@role('*')
//Has any roles
@else
//Has no role (Eg: role_id=0)
@endrole
Route::get('/', function () {
return "home ppage";
});
Route::group(['middleware' => ['auth','permission']], function () {
Route::get('/secret', function () {
return "SECRET PAGE";
});
Route::get('/account', function ($id) {
return "view account infos";
});
});
Route::group(["prefix" => "admin",'middleware' => ['auth','permission']], function () {
Route::get('/', function () {
return view('dashboard');
});
Route::ressource('posts', 'PostController');
});