首頁  >  文章  >  php框架  >  laravel中間件都有哪些

laravel中間件都有哪些

青灯夜游
青灯夜游原創
2021-09-14 17:41:403079瀏覽

中介軟體有:1、Authenticate;2、CheckForMaintenanceMode;3、EncryptCookies;4、RedirectIfAuthenticated;5、TrimStrings;6、TrustProxies等等。

laravel中間件都有哪些

本教學操作環境:windows7系統、Laravel6版、Dell G3電腦。

Laravel自帶的中間件

Laravel 自帶了一些中間件,包括身份驗證、CSRF 保護等。 Laravel 具體啟用了哪些中間件,可透過 app\Http\Kernel.php 檔案查看。對於以 \App\Http\Middleware\ 開頭的中間件(位於 app/Http/Middleware 目錄)是我們可以對其行為進行自訂的中間件。

Authenticate 中間件

來源檔:app\Http\Middleware\Http\Middleware\Authenticate.php

<?php
namespace App\Http\Middleware;
use Illuminate\Auth\Middleware\Authenticate as Middleware;
class Authenticate extends Middleware
{
    /**
     * Get the path the user should be redirected to when they are not authenticated.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return string
     */
    protected function redirectTo($request)
    {
        if (! $request->expectsJson()) {
            return route(&#39;login&#39;);
        }
    }
}

作用:

使用者身份驗證。可修改 redirectTo 方法,傳回未經驗證的使用者應該重定向到的路徑。

CheckForMaintenanceMode 中間件

來源檔案:app\Http\Middleware\CheckForMaintenanceMode.php

#
<?php
namespace App\Http\Middleware;
use Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode as Middleware;
class CheckForMaintenanceMode extends Middleware
{
    /**
     * The URIs that should be reachable while maintenance mode is enabled.
     *
     * @var array
     */
    protected $except = [
        //
    ];
}

作用:

偵測項目是否處於維護模式。可透過 $except 數組屬性設定在維護模式下仍能存取的網址。

EncryptCookies 中間件

來源檔案:app\Http\Middleware\EncryptCookies.php

#
<?php
namespace App\Http\Middleware;
use Illuminate\Cookie\Middleware\EncryptCookies as Middleware;
class EncryptCookies extends Middleware
{
    /**
     * The names of the cookies that should not be encrypted.
     *
     * @var array
     */
    protected $except = [
        //
    ];
}

作用

對Cookie 進行加解密處理與驗證。可透過 $except 數組屬性設定不做加密處理的 cookie。

RedirectIfAuthenticated 中間件

來源檔:app\Http\Middleware\RedirectIfAuthenticated.php

<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
class RedirectIfAuthenticated
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string|null  $guard
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->check()) {
            return redirect(&#39;/home&#39;);
        }
        return $next($request);
    }
}

作用:

當請求頁是註冊、登入、忘記密碼時,偵測使用者是否已經登錄,如果已經登錄,那麼就重新導向到首頁,如果沒有就開啟對應介面。可以在 handle 方法中自訂重定向到的路徑。

TrimStrings 中間件

來源檔:app\Http\Middleware\TrimStrings.php

<?php
namespace App\Http\Middleware;
use Illuminate\Foundation\Http\Middleware\TrimStrings as Middleware;
class TrimStrings extends Middleware
{
    /**
     * The names of the attributes that should not be trimmed.
     *
     * @var array
     */
    protected $except = [
        &#39;password&#39;,
        &#39;password_confirmation&#39;,
    ];
}

作用:

對請求參數內容進行前後空白字元清理。可透過 $except 陣列屬性設定不做處理的參數。

TrustProxies 中間件

原始檔:app\Http\Middleware\TrustProxies.php##

<?php
namespace App\Http\Middleware;
use Illuminate\Http\Request;
use Fideloper\Proxy\TrustProxies as Middleware;
class TrustProxies extends Middleware
{
    /**
     * The trusted proxies for this application.
     *
     * @var array|string
     */
    protected $proxies;
    /**
     * The headers that should be used to detect proxies.
     *
     * @var int
     */
    protected $headers = Request::HEADER_X_FORWARDED_ALL;
}

作用:

配置可信任代理程式。可透過 $proxies 屬性設定可信任代理列表,$headers 屬性設定用來偵測代理程式的 HTTP 頭字段。

VerifyCsrfToken 中間件

來源檔:

app\Http\Middleware\VerifyCsrfToken.php#

<?php
namespace App\Http\Middleware;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as Middleware;
class VerifyCsrfToken extends Middleware
{
    /**
     * Indicates whether the XSRF-TOKEN cookie should be set on the response.
     *
     * @var bool
     */
    protected $addHttpCookie = true;
    /**
     * The URIs that should be excluded from CSRF verification.
     *
     * @var array
     */
    protected $except = [
        //
    ];
}

作用:

驗證請求裡的令牌是否與儲存在會話中令牌相符。可透過 $except 陣列屬性設定不做 CSRF 驗證的網址。

相關推薦:

最新的五個Laravel影片教學

#

以上是laravel中間件都有哪些的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn