如何检索当前路由的中间件?
我试图通过检查中间件是否已添加到路由中,根据您是否位于网站的特定部分来设置异常处理程序以不同的方式工作。
<?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);
}
}
Run Code Online (Sandbox Code Playgroud)
如果我将当前路由转储到路由器内,它会显示我需要检查的中间件数组:dd(Route::current())但似乎没有访问当前路由器的方法,例如:$currentRouteMiddleware = Route::current()->router->getMiddleware();
pat*_*cus 11
根据您的需求,有几个选项。
如果您希望将所有中间件别名分配给该路由,您可以使用:
Route::current()->gatherMiddleware();
Run Code Online (Sandbox Code Playgroud)
这不会扩展指定的中间件组,因此结果可能类似于:
[
'web',
'admin.user'
]
Run Code Online (Sandbox Code Playgroud)
如果您希望将所有中间件类分配给该路由,您可以使用:
Route::gatherRouteMiddleware(Route::current());
Run Code Online (Sandbox Code Playgroud)
这将为您提供类,但不会有类的关联别名或组。结果可能如下所示:
[
"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"
]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6965 次 |
| 最近记录: |