Laravel middlware阵列

时间:2016-02-15 14:10:22

标签: php laravel-5.2

我正在使用Laravel。

我想在我的api中将数组传递给我的中间件,这样我就可以检查用户是否具有某个角色。这是我的想法:

 public function handle($request, Closure $next,$role)
    {
        $employee = Employee::find($request->parameters[0]['userid']);

        foreach($role as $r) {
            if (!$employee->hasRole($employee, $r)) {
                return response()->json([
                    'result' => [
                        'message' => 'Error creating Employee'
                    ]
                ], 500);
            }
        }
        return $next($request);
    }

但我怎样才能传递给我的中间件。现在我正在这样做:

Route::group(['middleware' => 'role:Administrator,intern'], function () {

});

1 个答案:

答案 0 :(得分:1)

您只能将字符串作为中间件参数传递,但您可以使用PHP将字符串转换为使用explode的数组:

public function handle($request, Closure $next, $role)
{
    $employee = Employee::find($request->parameters[0]['userid']);

    foreach(explode(',', $role) as $r) {
        if (!$employee->hasRole($employee, $r)) {
            return response()->json([
                'result' => [
                    'message' => 'Error creating Employee'
                ]
            ], 500);
        }
    }
    return $next($request);
}