【问题标题】:multiple controller middleware usiing same methods in Laravel 5.2在 Laravel 5.2 中使用相同方法的多个控制器中间件
【发布时间】:2017-09-23 03:27:51
【问题描述】:

我试图将同一控制器的某些方法与不同的中间件一起使用,例如让管理员和用户使用方法索引并在同一控制器中创建。所以我做了一个管理员守卫和用户守卫。

这是我的管理中间件

<?php

namespace App\Http\Middleware;

use Closure;
use Auth;

class AdminMidleware
{

    public function handle($request, Closure $next)
    {
        if (Auth::guard('admin')->guest()) {
            if ($request->ajax() || $request->wantsJson()) {
                return response('Unauthorized.', 401);
            } else {
                return redirect()->guest('/');
            }
        }

        return $next($request);
    }
}

我的用户中间件

<?php

namespace App\Http\Middleware;

use Closure;
use Auth;

class UserMiddleware
{

    public function handle($request, Closure $next)
    {
        if (Auth::guard('user')->guest()) {
            if ($request->ajax() || $request->wantsJson()) {
                return response('Unauthorized.', 401);
            } else {
                return redirect()->guest('/');
            }
        }

        return $next($request);
    }
}

它们都在 $routeMiddleware 的 kernel.php 中

'admin' => \App\Http\Middleware\AdminMidleware::class,
'user' => \App\Http\Middleware\UserMiddleware::class,

如果我使用它,我可以让只有管理员才能访问这些方法,并且它可以工作,你必须以管理员身份登录才能使用这些方法。

public function __construct(){
   $this->middleware('admin', ['only' => [
      'index',
      'create',
   ]]);
}

这是假设让用户和管理员都能够在同一个控制器中使用索引和创建方法,并作为第一个参数发送一个包含管理员和用户中间件的数组,

public function __construct(){
   $this->middleware(['admin', 'user'], ['only' => [
      'index',
      'create',
   ]]);
}

但是,它不起作用,它实际上使没有人能够使用这些方法,你能帮我把这个工作做好吗?我做错了什么?

【问题讨论】:

    标签: php laravel controller middleware guard


    【解决方案1】:

    由于您的中间件是相同的,除了使用的 guard 之外,您可以尝试制作一个 auth 中间件来模仿较新版本的 Laravel 附带的 auth 中间件。这将允许您将多个守卫作为参数传递给中间件。

    public function handle($request, Closure $next)
    {
        // get guards or use 'null' (default guard)
        $guards = array_slice(func_get_args(), 2) ?: [null];
    
        // spin through guards to find one that checks out
        foreach ($guards as $guard) {
            if (Auth::guard($guard)->check()) {
                // if we have a guard name, not null
                if ($guard) {
                    // use this guard as the default
                    Auth::shouldUse($guard);
                }
    
                // we have an authed user from some guard move along
                return $next($request);
            }
        }
    
        // handle no authed user
        if ($request->ajax() || $request->wantsJson()) {
            return response('Unauthenticated.', 401);
        }
    
        return redirect()->guest('/');
    }
    

    它将遍历所有传递的守卫,如果其中任何一个解析用户,则该守卫将成为Auth 将使用的默认守卫。

    $this->middleware('thatmiddleware:user,admin');
    

    类似的东西应该可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-26
      • 2014-08-26
      • 2017-01-18
      • 1970-01-01
      • 2019-03-09
      相关资源
      最近更新 更多