首页 > 解决方案 > 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.

标签: phplaravelpackage

解决方案


您需要bootAuthServiceProvider

阅读关于授权门的 Laravel 文档

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 类上运行代码,您只能声明方法、常量和属性...等


推荐阅读