PHP code example of despark / laravel-permission-roles

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

    

despark / laravel-permission-roles example snippets



'providers' => array(
  ...
  'Despark\LaravelPermissionRoles\LaravelPermissionRolesServiceProvider',
);



use Despark\LaravelPermissionRoles\PermissionTrait;

class User extends Eloquent
{
    use PermissionTrait;
}



$input = Input::all();
$user = new User();
$user = $user->create($input);
$user->attachRole(array_get($input, 'role'));



$user = User::findOrFail($id)
$user->changeRole(Input::get('role'));



use Despark\LaravelPermissionRoles\Role;

class RoleController extends BaseController
{
	Role::findOrFail($id);
}



$input = Input::all();
$role = Role::findOrFail($id);
$role->update($input);
if ($role->permissions->count()) {
    $role->permissions()->detach($role->permissions->lists('id'));
    $role->permissions()->attach(array_get($input, 'permissions'));
}



Auth::user()->canLoginToAdmin()  // permission slug 'login_to_admin'

Auth::user()->canAddUsers()  // permission slug 'edit_user'

Auth::user()->canEditUsers()  // permission slug 'edit_page'



Route::filter('admin.permission', function () {

    $route = explode('/', \Route::getCurrentRoute()->getPath());

    if (!Auth::user()->can('edit_'.str_replace('-', '_', array_get($route, 1)))) {
        throw new \Exception("Sorry, you don't have permission to access this page.");
    }
});



Route::group(
    ['before' => 'admin.permission'], function () {
        Route::resource('user', 'UserController');
    }
);

Route::group(
    ['before' => 'admin.permission'], function () {
        Route::resource('page', 'PageController');

    }
);