1. 程式人生 > >Laravel 5.5框架 核心類Kernel

Laravel 5.5框架 核心類Kernel

<?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 = [ // 檢測是否進入[維護模式] // 見:https://d.laravel-china.org/docs/5.5/configuration#maintenance-mode \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class, // 檢測請求的資料是否過大 \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class, // 對提交的請求引數進行 php 函式 `trim()` 處理
\App\Http\Middleware\TrimStrings::class, // 將提交的請求引數中空子串轉換為 null \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class, // 修正代理伺服器後的伺服器引數 \App\Http\Middleware\TrustProxies::class, ]; /** * The application's route middleware groups. * 定義中介軟體組 * @var
array */
protected $middlewareGroups = [ // web 中介軟體組,應用於 route/web.php 路由檔案 'web' => [ // cookie 加密解密 \App\Http\Middleware\EncryptCookies::class, // 將 cookie 新增到響應中 \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, // 開啟會話 \Illuminate\Session\Middleware\StartSession::class, // 認證使用者,此中介軟體以後 Auth 類才能生效 // 見:https://d.laravel-china.org/docs/5.5/authentication \Illuminate\Session\Middleware\AuthenticateSession::class, // 將系統的錯誤資料注入到檢視變數 $errors 中 \Illuminate\View\Middleware\ShareErrorsFromSession::class, // 檢驗 CSRF ,防止跨站請求偽造的安全威脅 // 見:https://d.laravel-china.org/docs/5.5/csrf \App\Http\Middleware\VerifyCsrfToken::class, // 處理路由繫結 // 見:https://d.laravel-china.org/docs/5.5/routing#route-model-binding \Illuminate\Routing\Middleware\SubstituteBindings::class, ], // API 中介軟體組,應用於 routes/api.php 路由檔案 'api' => [ // 使用別名來調中介軟體 // 請見:https://d.laravel-china.org/docs/5.5/middleware#為路由分配中介軟體 'throttle:60,1', 'bindings', ], ]; /** * The application's route middleware. * * These middleware may be assigned to groups or used individually. * 中介軟體別名設定,允許你使用別名呼叫中介軟體,例如上面的 api 中介軟體組呼叫 * @var array */ protected $routeMiddleware = [ // 只有登入使用者才能訪問,我們在控制器的構造方法中大量使用 'auth' => \Illuminate\Auth\Middleware\Authenticate::class, // HTTP Basic Auth 認證 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, // 處理路由繫結 // 見:https://d.laravel-china.org/docs/5.5/routing#route-model-binding 'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class, // 使用者授權功能 'can' => \Illuminate\Auth\Middleware\Authorize::class, // 只有遊客才能訪問,在register 和 login 請求中使用,只有未登入的使用者才能訪問這些頁面 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, // 訪問節流,類似於 [1分鐘只能請求 10 次]的需求,一般在 API 中使用 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, ]; }