我试图通过身份验证中间件在我的 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/