gpt4 book ai didi

Laravel 获取当前路由的中间件

转载 作者:行者123 更新时间:2023-12-05 08:48:42 25 4
gpt4 key购买 nike

如何获取当前路由的中间件?

我正在尝试通过检查是否已将中间件添加到路由来根据您是否位于网站的特定部分来设置异常处理程序以不同方式工作。

<?php

namespace App\Exceptions;

//use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Abrigham\LaravelEmailExceptions\Exceptions\EmailHandler as ExceptionHandler;

use Illuminate\Routing\Exceptions\InvalidSignatureException;
use Illuminate\Support\Facades\Route;
use Throwable;

class Handler extends ExceptionHandler
{
/**
* A list of the exception types that are not reported.
*
* @var array
*/
protected $dontReport = [
//
];

/**
* A list of the inputs that are never flashed for validation exceptions.
*
* @var array
*/
protected $dontFlash = [
'password',
'password_confirmation',
];

/**
* Report or log an exception.
*
* @param \Throwable $exception
* @return void
*
* @throws \Exception
*/
public function report(Throwable $exception)
{
parent::report($exception);
}

/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Throwable $exception
* @return \Symfony\Component\HttpFoundation\Response
*
* @throws \Throwable
*/
public function render($request, Throwable $exception)
{
switch(true) {
case $exception instanceof \Illuminate\Session\TokenMismatchException:
// Redirect back if form invalid
return redirect()
->back()
->withInput($request->except($this->dontFlash))
->withErrors('The form has expired due to inactivity. Please try again');

break;
case $exception instanceof \Illuminate\Database\Eloquent\ModelNotFoundException:
// Redirect back with message if model not found error
$redirect = app('redirect');

// Check for different url to prevent redirect loop
if (request()->fullUrl() === $redirect->back()->getTargetUrl()){

// $currentRouteMiddleware = request()->route()->controllerMiddleware() returns empty array
// $currentRouteMiddleware = Route::current()->router->getMiddleware(); router is private

$response = $redirect->to(isset($currentRouteMiddleware['admin.user']) ? '/admin' : '/');
} else {
$response = $redirect->back();
}

return $response
->withInput($request->except($this->dontFlash))
->withErrors('That page could not be found. Please try again or report the broken link: ' . $request->getRequestUri());

break;
}

return parent::render($request, $exception);
}
}

如果我将当前路由转储到路由器中,它会显示我需要检查的中间件数组:dd(Route::current()) 但似乎没有办法访问当前路由器,例如:$currentRouteMiddleware = Route::current()->router->getMiddleware();

route dump

最佳答案

根据您要查找的内容,有几个选项。

如果你想把所有的中间件别名分配给路由,你可以使用:

Route::current()->gatherMiddleware();

这不会扩展分配的中间件组,因此结果可能类似于:

[
'web',
'admin.user'
]

如果你想把所有的中间件类都分配给路由,你可以使用:

Route::gatherRouteMiddleware(Route::current());

这将为您提供类,但不会为这些类提供关联的别名或组。结果可能如下所示:

[
"App\Http\Middleware\EncryptCookies",
"Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse",
"Illuminate\Session\Middleware\StartSession",
"Illuminate\View\Middleware\ShareErrorsFromSession",
"App\Http\Middleware\VerifyCsrfToken",
"Illuminate\Routing\Middleware\SubstituteBindings",
"TCG\Voyager\Http\Middleware\VoyagerAdminMiddleware"
]

关于Laravel 获取当前路由的中间件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65554128/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com