我've got Backpack for Laravel installed and have been using it for some time as an admin back end on a project. I'米也用spatie/permission module(可能带有背包,不记得了)为前端创建用户 .
目前,所有用户都能够访问前端和后端,无论他们属于哪个组 . 为了分离前端会话和后端会话,我已经按照'd like to change that so that only members in an 1425446 group are able to access the back end. I'进行了分析,但这并不是我想要的,因为所有用户仍然可以访问项目的两个站点 .
我猜我需要为CRUD路线添加一个后卫,但我发现它要比它应该更难 . 任何关于如何做到这一点的指针将不胜感激 . TIA .
1 回答
您可以创建新的中间件并在路由组中将其用于管理路由 .
要创建一个新的中间件,请使用
php artisan
命令:(您可以将新的中间件命名为您想要的:php artisan make:middleware RequireAdminRole
现在,在您的新中间件中,在
handle
函数中,您可以使用类似这样的内容返回403 Forbidden错误消息:这里我们使用hasRole方法,但您可以使用更多 . See the spatie/laravel-permissions documentation for more info.
现在,让's assign this middleware a ' name',以便我们可以在管理员的路由组中使用它 . 在
App\Kernel.php
文件中,最后,在$routeMiddleware
数组内添加它并给它一个新的,例如:'isadmin' => \App\Http\Middleware\RequireAdminRole::class,
最后,您可以将此中间件添加到您的管理路由组(如果您使用的是最新的背包3.4版本,则应该在
custom.php
文件中):现在,您对管理路由的所有请求都应受到用户角色检查的保护 .
如果您遇到任何问题,请告诉我们您的具体情况 .
最好的祝福,
〜克里斯蒂安