如何在Laravel中访问两个中间件的多个路由

时间:2019-09-22 07:51:59

标签: php laravel laravel-5.8 laravel-middleware

你好,我在如何访问默认auth和tsr中的相同路由方面陷入了困境。我的auth.php如下所示:-

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'api' => [
        'driver' => 'token',
        'provider' => 'users',
        'hash' => false,
    ],
    'tsr' => [
        'driver' => 'session',
        'provider' => 'tsrs',
    ],
],

这是我下面的web.php文件:-

 //Tsr Routes
 Route::get('/tsr', 'Tsr\TsrController@index')->name('tsrs.dashboard');
 Route::get('/tsr/login', 'Auth\Tsr\LoginController@login')->name('tsrs.auth.login');
 Route::post('tsr/login', 'Auth\Tsr\LoginController@loginAdmin')->name('tsrs.auth.loginAdmin');

Route::group(['middleware' => ['auth', 'auth:tsr'], 'prefix' => 'admin', 'as' => 'admin.'], function () {
 Route::get('/home', 'HomeController@index')
 Route::resource('regular_meetings', 'Admin\RegularMeetingsController');
 Route::post('regular_meetings_mass_destroy', ['uses' => 'Admin\RegularMeetingsController@massDestroy', 'as' => 'regular_meetings.mass_destroy']);
});

Kernel.php如下:-

protected $routeMiddleware = [
    'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
    'can' => \Illuminate\Auth\Middleware\Authorize::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
    'approved' => \App\Http\Middleware\ApproveMiddleware::class,
    'usergate' => \App\Http\Middleware\UserGate::class,
];

下面的中间件:-

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class RedirectIfAuthenticated
{
/**
 * Handle an incoming request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Closure  $next
 * @param  string|null  $guard
 * @return mixed
 */
public function handle($request, Closure $next, $guard = null)
{
    if (Auth::guard($guard)->check()) {
        return redirect('/admin/home');
    }

    return $next($request);
  }
}

现在您看到我添加了三个路线,所有两个中间人都可以访问。

现在的问题是,无法通过访问两个已登录用户的路由来登录。如果从中间件删除auth:tsr,它将仅对auth用户起作用。 Tsr登录视图和用户登录视图不同。请帮助我解决此问题。

0 个答案:

没有答案