Download the PHP package saritasa/roles-simple without Composer
On this page you can find all versions of the php package saritasa/roles-simple. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download saritasa/roles-simple
More information about saritasa/roles-simple
Files in saritasa/roles-simple
Package roles-simple
Short Description User has field role_id and corresponding lookup table
License MIT
Informations about the package roles-simple
Saritasa Roles for Laravel
Simplified implementation of user roles for Laravel applications: User has role_id field, and corresponding roles lookup table exists.
Usage
Install the package:
If you use Laravel 5.4 or less,
or 5.5+ with package discovery disabled,
add the RolesServiceProvider service provider in config/app.php
:
Then publish DB migrations:
Available classes
HasRoles
Provides hasRole method;
Example:
then somewere in code:
hasRole($role) method can accept either role ID (integer) or role slug (string). Using role ID is a bit faster, because does not require reading role record from lookup table.
Role (model)
You can use built-in class Saritasa\Roles\Models\Role to list of models or create new roles in migrations or in code.
Role model contains 3 fields:
- id (integer) - primary key
- name (string) - supposed to be visible to user and may change, as needed. May contain any characters (including spaces), in any case. Do not use role name as identifier (ex. to search by it).
- slug (string) - human-readable role identifier, supposed to be constant, while name can be changed. You can reference role by slug in code (ex. in hasRole() method). It's recommended to keep slugs in lowercase, use underscore or dash instead of spaces.
Roles (enum)
Package contains enum Saritasa\Roles\Enums\Roles, which has 2 predefined roles: User and Admin, which is suitable for many applications.
But you are not limited to these roles, you can define your own enum (extending this one or create new from scratch) and use it.
Middleware
You can use middleware in routes to limit access to certain pages:
Middleware with alias is registered by service provider, no need to register it manually.
Format is role:role_slug1,role_slug2,role_slug3.
If user does not have any of required roles, AccessDeniedHttpException will be thrown
Contributing
- Create fork, checkout it
- Develop locally as usual. Code must follow PSR-1, PSR-2 - run PHP_CodeSniffer to ensure, that code follows style guides
- Cover added functionality with unit tests and run PHPUnit to make sure, that all tests pass
- Update README.md to describe new or changed functionality
- Add changes description to Semantic Versioning convention to determine next version number.
- When ready, create pull request
Make shortcuts
If you have GNU Make installed, you can use following shortcuts:
- (instead of ) - run static code analysis with PHP_CodeSniffer to check code style
- (instead of ) - fix code style violations with PHP_CodeSniffer automatically, where possible (ex. PSR-2 code formatting violations)
- (instead of ) - run tests with PHPUnit
-
- instead of
- or just without parameters - invokes described above install, cs, test tasks sequentially - project will be assembled, checked with linter and tested with one single command
Resources
- Bug Tracker
- Code
- Changes History
- Authors
All versions of roles-simple with dependencies
illuminate/database Version ^5.4 || ^6.0
illuminate/support Version ^5.4 || ^6.0
illuminate/http Version ^5.4 || ^6.0
saritasa/php-common Version ^1.0