---
### 1. 中間件技術

> ### 中間件技術提供一種攔截的作用 (面向切面的思想)
#### 1.1 創建一個中間件
docker-compose exec workspace bash 進入 docker 容器中
php artisan make:middleware Benchmark
> 出現 `Middleware created successfully.`
---
<center></center>
---
#### 1.2 前置和后置中間件
> ##### 前置: `請求處理之前`
> ##### 后置:`請求處理之后`
#### 在 handle 方法中出現 , 在中間件里面我們可以統一的做很多事情攔截 Request
public function handle($request, Closure $next)
{
//前置
$sTime = microtime(true);
$response = $next($request);
//后置
$runTime = microtime(true) - $sTime; //執行時間
Log::info('banchmark', [ //記錄日志
'url' => $request->url(),
'input' => $request->input(),
'time' => "runTimes: " . $runTime
]);
return $response;
}
#### 1.3 注冊全局中間件與路由中間件 `Kernel.php`
> ##### 注冊 `全局中間件`
//全局中間件每一個中間件都會去請求
protected $middleware = [
Benchmark::class
];
> ##### 注冊后查看打印的日志
tail -f storage/logs/laravel.log
> #### 這個時候我們隨便訪問一個路由可以看到日志在根據剛剛我們定義的日志規則輸出
---
> ##### 注冊 `路由中間件` 在路由文件中定義這里我們用剛剛的 web.php
Route::get('/home/hello', 'HomeController@hello')->middleware(\App\Http\Middleware\Benchmark::class);
#### 同樣我們查看日志訪問 /home/hello 后日志記錄, 而其他地方則不會
> #### 現在我們放到路由 routeMiddleware中 `kernel.php`
protected $routeMiddleware = [
'benchmark' => \App\Http\Middleware\Benchmark::class
];
//同樣我們需要修改路由的名稱
Route::get('/home/hello', 'HomeController@hello')->middleware('benchmark');
> #### 我們發現這個路由是在 web 這個路由組下面 同樣也是在 `kernal.php` 中 middlewareGroups
protected $middlewareGroups = [
'web' => [
'benchmark' => \App\Http\Middleware\Benchmark::class
],
];
##### `提示:這里就把 web.php 中的路由改為原來的就可以了`
---
### 1.4 在構造函數中注冊中間并過濾路由
#### 設置黑名單 `except` 以上是不經過這個路由
#### 設置白名單 `only` 只有這個路由生效
class HomeController extends Controller
{
public function __construct()
{
$this->middleware(Benchmark::class, ['except' => ['hello']]); // 也可以寫 except
}
public function hello()
{
return 'hello';
}
}
> #### `注意: 這里都在 kernel 需要把其他都注冊刪除掉可以留一個 $routeMiddleware`
#### 如何在構造函數中給中間件傳參
//傳遞參數
$this->middleware('benchmark:test1, test1', ['except' => ['hello']]);
---
接受參數
public function handle($request, Closure $next, $a , $b)
{
//前置
$sTime = microtime(true);
$response = $next($request);
//后置
$runTime = microtime(true) - $sTime; //執行時間
Log::info('banchmark', [ //記錄日志
'url' => $request->url(),
'a' => $a,
'b' => $b,
'input' => $request->input(),
'time' => "runTimes: " . $runTime
]);
return $response;
}
---
### 2. Laravel 的中間件技術
#### `全局中間件說明`
protected $middleware = [
// \App\Http\Middleware\TrustHosts::class,
\App\Http\Middleware\TrustProxies::class, //可信任代理獲取真正客戶端的信息
\Fruitcake\Cors\HandleCors::class, //跨域
\App\Http\Middleware\CheckForMaintenanceMode::class, //是否在維護狀態
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class, //檢查 post 包體的大小過大就報錯
\App\Http\Middleware\TrimStrings::class, //把參數去空格
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class, // 把空字符串轉換為 null
];
#### `中間件組說明`
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class, //處理 cookies 加密解密
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, //把 cookies 加入到 response 中
\Illuminate\Session\Middleware\StartSession::class, //處理 session
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
// \App\Http\Middleware\VerifyCsrfToken::class, // 設置 CSR 防止 CSR 攻擊 驗證 token 合法才能訪問 保證安全性 調試的時候就注釋, api 范文沒必要, web 訪問可以打開
\Illuminate\Routing\Middleware\SubstituteBindings::class, //顯示或者隱私的 轉換對象
],
'api' => [
'throttle:60,1', //限流一分鐘60次
\Illuminate\Routing\Middleware\SubstituteBindings::class, //
],
];
---
#### `路由中間件`(略)
---
#### 中間件的優先級
protected $middlewarePriority = [
];
### 總結
> #### 三種分配方式
##### 1. 全局
##### 2. 分組
##### 3. 控制器