SimpleRoles
![Software License][ico-license]
![Coverage Status][ico-scrutinizer]
![Total Downloads][ico-downloads], (*1)
SimpleRoles is a simple way to have roles in your laravel projects., (*2)
, (*3)
Installation
Via Composer, (*4)
``` bash
$ composer require merodiro/simple-roles, (*5)
Run the command to publish the package migration
```bash
php artisan vendor:publish --provider="Merodiro\SimpleRoles\SimpleRolesServiceProvider"
Migrate database, (*6)
It assumes that users are in the users
table, if not you can change the config file, (*7)
php artisan migrate
add middleware in app/Http/Kernel.php
, (*8)
protected $routeMiddleware = [
...
'role' => \Merodiro\SimpleRoles\Middleware\RoleMiddleware::class,
];
Usage
add roles to roles
array in simple-roles config file first, (*9)
Set Role
$user->setRole('admin');
Remove Role
$user->removeRole();
Has Role
if($user->hasRole('admin')){
// do something
}
Blade Templates
to show content to admins only, (*10)
@role('admin')
<h3>this is visible to admins only</h3>
@endrole
to show different content to admins and non-admins users, (*11)
@role('admin')
<h3>this is visible to admins only</h3>
@else
<h3>this is visible to non admins only</h3>
@endrole
middleware
you can use middleware to limit accessing a certain route to admins only, (*12)
Route::get('/admin', function () {
...
})->middleware('role:admin');
Testing
bash
$ composer test
, (*13)
Contributing
Please see CONTRIBUTING and CODE_OF_CONDUCT for details., (*14)
Security
If you discover any security-related issues, please email merodiro@gmail.com instead of using the issue tracker., (*15)
Credits
License
The MIT License (MIT). Please see License File for more information., (*16)