将自定义角色分配给控制器Laravel Voyager
我在Laravel Voyager仪表板上创建了一个自定义页面,但是在限制其访问权限时遇到了困难。 使用Voyager文档:
我在权限表中创建密钥“ browse_sendusermails”,并链接到管理员角色
路线
Route::get('/sendusermails', [App\Http\Controllers\SendUserMailsController::class, 'index'])->middleware('admin.user');
控制器,
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use TCG\Voyager\Facades\Voyager;
class SendUserMailsController extends Controller
{
protected function guard()
{ return Auth::guard(app('VoyagerGuard')); }
public function index()
{
$this->authorize('browse_sendusermails');
return view('logs.logs');
}
}
但是当我单击 http://127.0.0.1:8000/admin/sendusermails 它总是显示403此操作未经授权。
我还能做什么来为控制器分配自定义角色?
$ this->授权('sendusermails');
也不起作用。
当然,当我删除$ this->授权('browse_sendusermails')
页面加载而没有问题
I created a custom page in the Laravel Voyager dashboard but I'm having trouble restricting its access rights.
Using the voyager documentation:
I create key "browse_sendusermails" in permissions table and linked to the admin role
Route
Route::get('/sendusermails', [App\Http\Controllers\SendUserMailsController::class, 'index'])->middleware('admin.user');
Controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use TCG\Voyager\Facades\Voyager;
class SendUserMailsController extends Controller
{
protected function guard()
{ return Auth::guard(app('VoyagerGuard')); }
public function index()
{
$this->authorize('browse_sendusermails');
return view('logs.logs');
}
}
But when i click http://127.0.0.1:8000/admin/sendusermails
It always show 403 This action is unauthorized.
What else can I do to assign custom roles to the controller?
$this->authorize('sendusermails');
also doesn't work.
Of course when I delete $this->authorize('browse_sendusermails')
the page loads without a problem
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您只需要在
AppServiceProvider
文件中添加门You just need to add the gate in
AppServiceProvider
file对于$ this->授权('browse_sendusermails');作品您需要第二个参数作为模型的实例:
但是,如果您有一个自定义门,
则不需要以下方式将模型作为第二个参数传递。
如果您在基本控制器中工作(作为您的情况),此外还将有效:
for $this->authorize('browse_sendusermails'); works you need a second param as a instance of model like:
But if you have a custom Gate like:
Is not need pass a model as second argument.
Additional if you working in a basic controller (as your case), with this will work: