在laravel中没有包的情况下将一个路由分配给多个用户 [英] Assigning one route to multiple user without a package in laravel
问题描述
我创建了四个用户类型 admin,供应商,员工,客户
.在用户迁移文件中,我具有以下内容:
I have created four user type admin,vendor,employee,customer
. In the user migration file I have the following:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->char('contact',24)->nullable();
$table->string('password');
$table->enum('roles',['admin', 'vendor', 'employee', 'customers']);
$table->string('image')->nullable();
$table->timestamps();
});
}
我已经为所有用户类型创建了中间件.管理员中间件
I have already created middleware for all user type. Admin middleware
public function handle($request, Closure $next)
{
if ($request->user()->roles == 'admin'){
return $next($request);
} else {
return redirect()->route($request->user()->roles);
}
}
客户中间件
public function handle($request, Closure $next)
{
if ($request->user()->roles == 'customer'){
return $next($request);
} else {
return redirect()->route($request->user()->roles);
}
}
供应商中间件
public function handle($request, Closure $next)
{
if ($request->user()->roles == 'vendor'){
return $next($request);
} else {
return redirect()->route($request->user()->roles);
}
}
员工中间件
public function handle($request, Closure $next)
{
if ($request->user()->roles == 'employee'){
return $next($request);
} else {
return redirect()->route($request->user()->roles);
}
}
我已将所有中间件包含在 Kernel.php
中.现在,我想将一条路由分配给多个用户.例如,管理员和供应商可以添加
, edit
, update
,删除
产品,而员工只能编辑
,他/他既不能添加
,也不能删除
.我只想将敏感路由发送给管理员.
I have included all the middleware in the Kernel.php
.
Now I want to assign one route to multiple users. For example, Admin and vendor can add
, edit
, update
, delete
product while Employee can only edit
, s/he can neither add
nor delete
. I want to sensitive route to admin only.
高度感谢您的解释!
推荐答案
也许这是另一种更好的解决方案,而不是具有多次中间战争.顺便说一句,他们都在做同样的事情.因此构建一个中间件并传递您需要的使用角色:)这个链接是一个很好的简单示例:
maybe another this solution is better rather than having multiple middlewars. by the way all them kind of doing same thing. so build a middleware and pass the use roles you need :) this link is great and simple example:
https://github.com/spatie/laravel-permission/blob/master/src/Middlewares/RoleMiddleware.php
所以您将拥有:
Route::middleware('roles:admin|customer')->get('/user', function () {});
这篇关于在laravel中没有包的情况下将一个路由分配给多个用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!