此工具包开箱即用,集成了接口级别的权限控制模块,自带模型、数据表、中间件,只需要配置数据库账户密码即可使用。
COMPOSER_MEMORY_LIMIT=-1 composer require --no-cache niro/rbac-backend
-
php
需要开启mongodb
扩展 -
需要名为
login
的路由,例如
Route::post('auth/login', [C\Auth\LoginController::class, 'login'])->name('login');
-
检查是否在
env
中配置了mongodb
数据库账密 -
检查
config/database.php
的connection
是否配置了mongodb
,如下案例
'connections' => [
...
'mongodb' => [
'driver' => 'mongodb',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', 27017),
'database' => env('DB_DATABASE', 'chatbot'),
'username' => env('DB_USERNAME', ''),
'password' => env('DB_PASSWORD', ''),
'options' => [
// here you can pass more settings to the Mongo Driver Manager
// see https://www.php.net/manual/en/mongodb-driver-manager.construct.php under "Uri Options" for a list of complete parameters that you can use
// 'database' => env('DB_AUTHENTICATION_DATABASE', 'admin'), // required with Mongo 3+
],
],
],
-
执行
php artisan migrate
-
执行
php artisan rbac:init
-
恭喜你,完成了初始化
- 可以自行创建用户模型,但必须继承
Rbac\Models\AdminUser
,并修改configs/auth.php
中的providers.admin-users.model
为您自定义的模型类
请将 src/Controllers/AdminUserController.php
文件拷贝到你想存放的路径,记得修改其中的命名空间