什麼是中介軟體,概念性東西用文字描述是不易懂得。直接看案例。
在接收請求引數,通常會使用trim()函式過濾引數兩邊的多餘空格。
laravel框架預設全域性中介軟體\App\Http\Middleware\TrimStrings::class
中介軟體會在請求到達控制器之前【也稱為 前置中介軟體】,進行trim()處理。也意味著你不再需要進行引數trim過濾。
<?php
namespace Illuminate\Foundation\Http\Middleware;
class TrimStrings extends TransformsRequest
{
/**
* The attributes that should not be trimmed.
*
* @var array
*/
protected $except = [
//
];
/** (備註:此函式在父類TransformsRequest中)
* Handle an incoming request. * * @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/public function handle($request, Closure $next)
{
$this->clean($request);
return $next($request);
}
/**
* Transform the given value.
*
* @param string $key
* @param mixed $value
* @return mixed
*/
protected function transform($key, $value)
{
if (in_array($key, $this->except, true)) {
return $value;
}
return is_string($value) ? trim($value) : $value;
}
}
前置中介軟體: 在請求到達控制器之前執行(用得比較多),
後置中介軟體:在請求到達控制器執行完後執行(用得少一點)
php artisan make:middleware EnsureTokenIsValid
此命令將在您的 app/Http/Middleware 目錄中放置一個新的 EnsureTokenIsValid 類。
在這個中介軟體中,如果提供的 token 輸入匹配指定的值,我們將只允許訪問路由。否則,我們會將使用者重定向回 home URI:
<?php
namespace App\Http\Middleware;
use Closure;
class EnsureTokenIsValid
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
//前置中介軟體
if ($request->input('token') !== 'my-secret-token') {
return redirect('home');
}
return $next($request);
}
/*
public function handle($request, Closure $next)
{
//後置中介軟體
$response = $next($request);
//do something
return $response;
}
*/
}
全域性中介軟體
如果您希望在對應用程式的每個 HTTP 請求期間執行中介軟體,請在 app/Http/Kernel.php 類的 $middleware 屬性中列出中介軟體類。
<?php
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
/**
* The application's global HTTP middleware stack.
*
* These middleware are run during every request to your application.
*
* @var array
*/
protected $middleware = [
\App\Http\Middleware\TrustHosts::class,
\App\Http\Middleware\TrustProxies::class,
\Fruitcake\Cors\HandleCors::class,
\App\Http\Middleware\PreventRequestsDuringMaintenance::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
\App\Http\Middleware\EnsureTokenIsValid::class,
];
將中介軟體分配給路由
如果您想將中介軟體分配給特定的路由,您應該首先在應用程式的
app/Http/Kernel.php
檔案中為中介軟體分配一個鍵。
// 在 App\Http\Kernel 類中...
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
'ensure.token' => \App\Http\Middleware\EnsureTokenIsValid::class,
];
其他使用細節查閱文件即可:中介軟體《Laravel 9 中文文件》
本作品採用《CC 協議》,轉載必須註明作者和本文連結