laravel - 将 'auth' 中间件与子域一起使用

标签 laravel authentication routes subdomain middleware

我试图通过身份验证中间件在我的 Controller 中传递某些功能。但是,与这些功能关联的路由需要 URL 中的子域才能工作。这些函数在不通过中间件或通过自定义中间件传递时似乎可以工作,但“auth”中间件会引发错误:

UrlGenerationException.php 第 17 行中的 UrlGenerationException:
缺少 [Route: login] [URI: login] 的必需参数。


这类似于当我尝试在参数列表中没有 $account 变量的情况下运行函数时抛出的错误(因为它需要一个带有路由的子域)。

在深入研究由 'auth' 中间件调用的 Authenticate 类后,我发现导致错误的行位于 public function authenticate(array $guards) 中,如下所示。

protected function authenticate(array $guards)

{
    if (empty($guards)) {
        /** ANYTHING BEFORE THIS RETURN STATEMENT RUNS WITH NO ERRORS **/
        return $this->auth->authenticate();
    }

    foreach ($guards as $guard) {
        if ($this->auth->guard($guard)->check()) {
            return $this->auth->shouldUse($guard);
        }
    }

    throw new AuthenticationException('Unauthenticated.', $guards);
}

这是我的一些代码供引用(仅包含代码的相关片段):

路由文件

路线\web.php
Route::group(['domain' => '{account}.'.env('APP_URL')], function () {
  include('appRoutes/bcm.php');
});

路线\appRoutes\bcm.php
/********************************************/
/*********STATIC PAGE ROUTES******************/
/********************************************/

Route::get('/boards-commissions', 'BCM\PagesController@home');


/********************************************/
/*********BOARD ROUTES***********************/
/********************************************/

Route::get('/boards-commissions/boards', 'BCM\BoardsController@index');

Route::get('/boards-commissions/boards/archive/index', 
'BCM\BoardsController@archiveIndex');

Controller

app\Http\Controllers\BCM\BoardsController.php
namespace App\Http\Controllers\BCM;

use Auth;
use Session;
use Validator;

use Illuminate\Http\Request;
use Illuminate\Foundation\Validation\ValidatesRequests;

//Models
use App\Models\BCM\Board;

class BoardsController extends Controller
{
    public function __construct()
    {
        $this->middleware('bcm_app_access');
        $this->middleware('auth', ['except' => array('index', 'show')]);
        $this->middleware('bcm_admin', ['only' => array('edit', 'update', 
'create', 'store', 'archive', 'showArchive', 'showArchivedBoard', 'restore')]);
    }

    public function index($account, Request $request) {
        $boards = Board::all();
        $user = $request->user();
        return view('bcm.boards.index')->with('boards', $boards)->with('user', $user);
    }

    public function archiveIndex($account, Request $request) {
        $boards = Board::all();
        $user = $request->user();
        return view('bcm.boards.archiveList')->with('boards', $boards)->with('user', $user);
    }
}

到目前为止,似乎只有 auth 中间件不起作用。即使它通过 bcm_app_access 中间件传递, index 函数也能正常运行(因为它被排除在 Controller 构造中的 auth 中间件之外)。但是archiveIndex在经过auth中间件时会抛出上面提到的UrlGenerationException错误。

我觉得 $account 参数需要添加或包含在某处,以便 auth 中间件知道它需要引用它,但我不确定在哪里添加它,我不知道其他文件是什么引用时 $this->auth->authenticate();正在运行,所以我不知道在哪里添加它。有什么想法吗?

最佳答案

一个粗略的 laravel7 指南:
通配符参数 {brand}您的子域:

Route::domain( '{brand}.' . parse_url(config('app.url'), PHP_URL_HOST) )->group(function () {

    // login page
    Route::get('/login', 'Auth\LoginController@showLoginForm')->name('login')->middleware('guest');

    Route::middleware('auth')->group(function () {
        Route::get('/', 'BrandController@showDashboard')->name('dashboard');
        
        ...
    }
});
将可以在 $request 中访问对象,所以在:
// app\Http\Middleware\Authenticate.php

protected function redirectTo($request)
{
    if (! $request->expectsJson()) {
        return route('login', $request->brand);
    }
}
只需将您的通配符作为参数传递给路由函数

关于laravel - 将 'auth' 中间件与子域一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43596330/

相关文章:

php - Laravel MySQL 匹配不返回结果

javascript - 通过ajax返回存储过程输出

laravel - 在 Laravel4 中通过数据透视表进行了许多操作

ruby-on-rails - 扩展 Devise SessionsController 以使用 JSON 进行身份验证

authentication - OAuth 失败了吗?

ios 导航与苹果或谷歌地图与多个目标

asp.net - ASP.NET 网站中的深度优先嵌套路由

php - Laravel 4 密码和 python

http - 使用 HTTPS 时 OAuth 是否无关紧要?

javascript - Angular (ui-router) 路由无法完全正常工作?无法直接访问 url 而不通过基本 url