Php Laravel 7在gates中的多个授权上,什么是不太冗长的代码?

Php Laravel 7在gates中的多个授权上,什么是不太冗长的代码?,php,laravel,laravel-7,Php,Laravel,Laravel 7,我正在为用户角色在App\Providers\AuthServiceProvider中定义门。用户对其角色有价值。比如:1,2,3,4用户管理中的角色赋予,所有用户都可以拥有多个授权 用户角色为: 人员编辑值=1 加上机构价值=2 用户管理值=3 添加记录值=4 为了从侧边栏中隐藏一些信息,我使用了Blade指令@can。 我需要根据每个用户的授权从侧边栏隐藏部分 我使用的有效且长期的方法是: AuthServiceProvider.php .... Gate::define(

我正在为用户角色在App\Providers\AuthServiceProvider中定义门。用户对其角色有价值。比如:1,2,3,4用户管理中的角色赋予,所有用户都可以拥有多个授权

用户角色为:

人员编辑值=1 加上机构价值=2 用户管理值=3 添加记录值=4 为了从侧边栏中隐藏一些信息,我使用了Blade指令@can。 我需要根据每个用户的授权从侧边栏隐藏部分

我使用的有效且长期的方法是:

AuthServiceProvider.php

....

        Gate::define('personnel-method', function ($user) {
        if(in_array(1,explode(",",$user->yetki)))
            return true;
        else
            return false;

    });

    Gate::define('add-method', function ($user) {
        if(in_array(2,explode(",",$user->yetki)))
            return true;
        else
            return false;

    });

    Gate::define('user-method', function ($user) {
        if(in_array(3,explode(",",$user->yetki)))
            return true;
        else
            return false;

    });

    Gate::define('adding-method', function ($user) {
        if(in_array(4,explode(",",$user->yetki)))
            return true;
        else
            return false;

    });

.....
sidebar.blade.php:

<ul>
 @can('personnel-method') <li>Personnel Edit </li> @endcan
 @can('add-method') <li>Add institution</li>@endcan
 @can('user-method')<li>User Management</li>@endcan
 @can('adding-method') <li>Adding Record</li>@endcan
</ul>

那么,是否有一个不那么冗长的代码来执行此操作?

要简化代码,您可以执行以下操作

    Gate::define('personnel-method', function ($user) {
        return in_array(1,explode(",",$user->yetki));
    });

    Gate::define('add-method', function ($user) {
        return in_array(2,explode(",",$user->yetki));
    });

    Gate::define('user-method', function ($user) {
        return in_array(3,explode(",",$user->yetki));
    });

    Gate::define('adding-method', function ($user) {
        return in_array(4,explode(",",$user->yetki))
    });

如果您使用的是PHP7.4,那么还可以使用arrow函数

    Gate::define('personnel-method', fn ($user) =>  in_array(1,explode(",",$user->yetki)));
    Gate::define('add-method', fn ($user) => in_array(2,explode(",",$user->yetki)));
    Gate::define('user-method', fn ($user) =>  in_array(3,explode(",",$user->yetki)));
    Gate::define('adding-method', fn ($user) => in_array(4,explode(",",$user->yetki)));


要简化代码,可以执行以下操作

    Gate::define('personnel-method', function ($user) {
        return in_array(1,explode(",",$user->yetki));
    });

    Gate::define('add-method', function ($user) {
        return in_array(2,explode(",",$user->yetki));
    });

    Gate::define('user-method', function ($user) {
        return in_array(3,explode(",",$user->yetki));
    });

    Gate::define('adding-method', function ($user) {
        return in_array(4,explode(",",$user->yetki))
    });

如果您使用的是PHP7.4,那么还可以使用arrow函数

    Gate::define('personnel-method', fn ($user) =>  in_array(1,explode(",",$user->yetki)));
    Gate::define('add-method', fn ($user) => in_array(2,explode(",",$user->yetki)));
    Gate::define('user-method', fn ($user) =>  in_array(3,explode(",",$user->yetki)));
    Gate::define('adding-method', fn ($user) => in_array(4,explode(",",$user->yetki)));

你可以用。您可以使用数组强制转换并将数据库中的值更改为JSON[1,2,3,4],或者定义一个将字符串拆分的数组

使用数组强制转换:

类用户扩展模型{ 受保护的$casts=[ 'yetki'=>'array', ]; //... } 因此,您可以将闸门更改为:

Gate::define('personnel-method', function ($user) {
    return $user->hasRole(UserRole::Personnel_Edit);
});
Gate::定义“人员方法”,函数$user{ 如果阵列1,$user->yetki 返回true; 其他的 返回false; }; 由于您正在检查布尔值,因此不必使用if,只需返回条件:

Gate::define('personnel-method', function ($user) {
  return in_array(1, $user->yetki);
});
为了使其更易于阅读,您可以将该逻辑移动到用户类上的方法:

最后,您可以定义类中的角色:

abstract class UserRole {
  const Personnel_Edit = 1;
  const Add_institution = 2;
  const User_Management = 3;
  const Adding_Record = 4;
}
并将闸门更改为:

Gate::define('personnel-method', function ($user) {
    return $user->hasRole(UserRole::Personnel_Edit);
});
你可以用。您可以使用数组强制转换并将数据库中的值更改为JSON[1,2,3,4],或者定义一个将字符串拆分的数组

使用数组强制转换:

类用户扩展模型{ 受保护的$casts=[ 'yetki'=>'array', ]; //... } 因此,您可以将闸门更改为:

Gate::define('personnel-method', function ($user) {
    return $user->hasRole(UserRole::Personnel_Edit);
});
Gate::定义“人员方法”,函数$user{ 如果阵列1,$user->yetki 返回true; 其他的 返回false; }; 由于您正在检查布尔值,因此不必使用if,只需返回条件:

Gate::define('personnel-method', function ($user) {
  return in_array(1, $user->yetki);
});
为了使其更易于阅读,您可以将该逻辑移动到用户类上的方法:

最后,您可以定义类中的角色:

abstract class UserRole {
  const Personnel_Edit = 1;
  const Add_institution = 2;
  const User_Management = 3;
  const Adding_Record = 4;
}
并将闸门更改为:

Gate::define('personnel-method', function ($user) {
    return $user->hasRole(UserRole::Personnel_Edit);
});

你说的快是什么意思?不太详细的代码?是的,是一个代码不太详细的方法。你认为这段代码正确吗?比如数组中的return,explode,,$user->yetki@谢谢你的提示。你说的快是什么意思?不太详细的代码?是的,是一个代码不太详细的方法。你认为这段代码正确吗?比如数组中的return,explode,,$user->yetki@ClémentBaconnier感谢您的提示。不幸的是,由于与我的队友一起开发,所以无法升级PHP版本。非常感谢您的关注和帮助。不幸的是,无法升级PHP版本,因为我的团队正在进行开发。非常感谢您的关心和帮助。