php - How to define a gate to allow admin access in WebDevEtc\BlogEtc package
问题描述
I want to use WebDevEtc\BlogEtc package to add blog section in my laravel website. I was install everything ok the problem is when I am add the following code in AuthServiceProvider as the developer explain I get the error syntax error, unexpected 'Gate' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST)
this is code tell as to add
Gate::define(\WebDevEtc\BlogEtc\Gates\GateTypes::MANAGE_BLOG_ADMIN, static function(?Model $user){
// Implement your logic here, for example:
return$user&&$user->email==='your-[email protected]';
// Or something like `$user->is_admin === true`
});
I was try to edit that code and start with
static function((?model ...........
and add admin email but still I get the error I was open issue in github but no respond. Anyone can help to put the code correct so as to work. am not much experience in laravel.
解决方案
您需要boot
在AuthServiceProvider
public function boot(): void
{
$this->registerPolicies();
\Gate::define(\WebDevEtc\BlogEtc\Gates\GateTypes::MANAGE_BLOG_ADMIN, static function(?\App\Models\User $user){
// Implement your logic here, for example:
return$user&&$user->email==='your-[email protected]';
// Or something like `$user->is_admin === true`
});
}
仅供参考:该错误与 Laravel 无关,您不能直接在顶级 PHP 类上运行代码,您只能声明方法、常量和属性...等
推荐阅读
- python - 使用 Gevent Monkey Patch 时 Keras 生成器冻结
- javascript - 在新选项卡中运行 js
- r - 如何获取列的最大值,当该列中的某个值在R中满足时重置最大值
- scala - elastic4s:在多个字段中搜索一个值
- python - 在 Python 中模拟加载时间变量
- java - 如果满足条件则删除java文件中的一行
- video - ffmpeg geq 过滤器无法识别按位运算符
- r - 如何在条形图而不是在条形图上调整条形百分比标签的高度?
- c++ - 复杂类型的模板函数
- javascript - 将 Promise Chaining 从 AngularJs 移植到 VueJs