向现有 lavarel lumen 项目添加角色和权限的 api 路由
<?php
namespace App\Models;
use Illuminate\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Laravel\Lumen\Auth\Authorizable;
use Spatie\Permission\Traits\HasRoles;
class User extends Model implements AuthenticatableContract, AuthorizableContract
{
use Authenticatable, Authorizable, HasFactory, HasRoles;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email',
];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = [
'password',
];
}
这是我的用户模型文件...
<?php
/** @var \Laravel\Lumen\Routing\Router $router */
// use Users\Http\Controllers\UserController;
$router->get('/', function () use ($router) {
// return $router->app->version();
return ENV('APP_NAME');
});
$router->get('/key', function() {
return \Illuminate\Support\Str::random(32);
});
// API route group
$router->group(['prefix' => 'api'], function () use ($router) {
// Matches "/api/register
$router->post('register', 'UserController@register');
// Matches "/api/login
$router->post('login','Users\Http\Controllers\UserController@login');
// User Details
$router->get('user-detail/{id}', 'Users\Http\Controllers\UserController@show');
// Users
$router->get('users', 'Users\Http\Controllers\UserController@index');
// Branches
$router->get('branches', 'Settings\Http\Controllers\BranchesController@index');
// Branch Details
$router->post('branch-detail', 'Settings\Http\Controllers\BranchesController@show');
// Orders
$router->get('orders', 'Orders\Http\Controllers\OrdersController@index');
// Order Details
$router->post('order-details', 'Orders\Http\Controllers\OrdersController@show');
// Order Create
$router->post('make-new-order', 'Orders\Http\Controllers\OrdersController@create');
// Order Tracking
$router->post('tracking', 'Orders\Http\Controllers\OrdersController@tracking');
// Get Delivery Cost
$router->post('delivery-cost', 'Settings\Http\Controllers\DeliveryController@delivery_cost');
});
这是我的 web.php 文件,我想在其中添加路由。
我使用 https://spatie.be 向现有的 lumen 项目添加了角色和权限/docs/laravel-permission/v5/installation-lumen。现在我想添加要编辑的角色和权限的选项(删除和添加)以及测试它们的方法(需要在 web.php 文件中添加路由)。我已将 Has Roles 添加到用户模型文件中,但尚未将角色选项添加到用户表中。然而,我发现的每个教程似乎都针对 Laravel 而不是 lumen 进行了微调,其中的步骤没有看到如何在 lumen 中执行更新内核等操作。这个 https://www.codecheef.org /article/laravel-spatie-roles-and-permissions-tutorial-from-scratch 是我尝试使用但无法完成的链接,因为有一些文件要求我编辑不在流明内的
<?php
namespace App\Models;
use Illuminate\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Laravel\Lumen\Auth\Authorizable;
use Spatie\Permission\Traits\HasRoles;
class User extends Model implements AuthenticatableContract, AuthorizableContract
{
use Authenticatable, Authorizable, HasFactory, HasRoles;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email',
];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = [
'password',
];
}
this is my users model file...
<?php
/** @var \Laravel\Lumen\Routing\Router $router */
// use Users\Http\Controllers\UserController;
$router->get('/', function () use ($router) {
// return $router->app->version();
return ENV('APP_NAME');
});
$router->get('/key', function() {
return \Illuminate\Support\Str::random(32);
});
// API route group
$router->group(['prefix' => 'api'], function () use ($router) {
// Matches "/api/register
$router->post('register', 'UserController@register');
// Matches "/api/login
$router->post('login','Users\Http\Controllers\UserController@login');
// User Details
$router->get('user-detail/{id}', 'Users\Http\Controllers\UserController@show');
// Users
$router->get('users', 'Users\Http\Controllers\UserController@index');
// Branches
$router->get('branches', 'Settings\Http\Controllers\BranchesController@index');
// Branch Details
$router->post('branch-detail', 'Settings\Http\Controllers\BranchesController@show');
// Orders
$router->get('orders', 'Orders\Http\Controllers\OrdersController@index');
// Order Details
$router->post('order-details', 'Orders\Http\Controllers\OrdersController@show');
// Order Create
$router->post('make-new-order', 'Orders\Http\Controllers\OrdersController@create');
// Order Tracking
$router->post('tracking', 'Orders\Http\Controllers\OrdersController@tracking');
// Get Delivery Cost
$router->post('delivery-cost', 'Settings\Http\Controllers\DeliveryController@delivery_cost');
});
this is my web.php file in which I want to add routes.
I added roles and permissions to an existing lumen project using https://spatie.be/docs/laravel-permission/v5/installation-lumen. Now I wanted to add the option for the roles and permissions to be edited(removing and adding) and a way for them to be tested(need to add the routes in the web.php file). I have added Has Roles to the users model file and I'm yet to add the roles options to the users table. However, every tutorial I've found seems to be finetuned for Laravel not lumen with steps that don't see how to do in lumen like update the kernel. This https://www.codecheef.org/article/laravel-spatie-roles-and-permissions-tutorial-from-scratch is the link I tried to use but couldn't finish because there's files I was asked to edit that were not in lumen
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论