ホームページ  >  記事  >  PHPフレームワーク  >  Laravelミドルウェアとは何ですか?

Laravelミドルウェアとは何ですか?

青灯夜游
青灯夜游オリジナル
2021-09-14 17:41:403080ブラウズ

ミドルウェアには、1. Authenticate、2. CheckForMaintenanceMode、3. EncryptCookies、4. RedirectIfAuthenticated、5. TrimStrings、6. TrustProxies などが含まれます。

Laravelミドルウェアとは何ですか?

このチュートリアルの動作環境: Windows 7 システム、Laravel 6 バージョン、Dell G3 コンピューター。

Laravel 独自のミドルウェア

Laravel には、認証、CSRF 保護などのいくつかのミドルウェアが付属しています。 Laravel によって具体的にどのミドルウェアが有効になっているかは、app\Http\Kernel.php ファイルで確認できます。 \App\Http\Middleware\ (app/Http/Middleware ディレクトリにあります) で始まるミドルウェアの場合、ミドルウェアの動作をカスタマイズできます。

ミドルウェアの認証

ソース ファイル: 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

<pre class="brush:php;toolbar:false">&lt;?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 = [ // ]; }</pre>関数:

プロジェクトがメンテナンス モードかどうかを検出します。メンテナンス モードでもアクセスできる URL は、$excel 配列プロパティを介して設定できます。

EncryptCookies ミドルウェア

ソース ファイル:

app\Http\Middleware\EncryptCookies.php

<pre class="brush:php;toolbar:false">&lt;?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 = [ // ]; }</pre>Function

Cookie を暗号化、復号化、検証します。暗号化されていない Cookie は、$excel 配列属性を介して設定できます。

RedirectIfAuthenticated ミドルウェア

ソース ファイル:

app\Http\Middleware\RedirectIfAuthenticated.php

<pre class="brush:php;toolbar:false">&lt;?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)-&gt;check()) { return redirect(&amp;#39;/home&amp;#39;); } return $next($request); } }</pre>関数:

リクエストページが登録、ログイン、またはパスワードを忘れた場合は、ユーザーがログインしているかどうかを確認し、ログインしている場合はホームページにリダイレクトし、ログインしていない場合は、対応するインターフェイスを開きます。リダイレクトするパスは、handle メソッドでカスタマイズできます。

TrimStrings ミドルウェア

ソース ファイル:

app\Http\Middleware\TrimStrings.php

<pre class="brush:php;toolbar:false">&lt;?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 = [ &amp;#39;password&amp;#39;, &amp;#39;password_confirmation&amp;#39;, ]; }</pre>関数:

リクエストパラメータの内容の前後の空白文字を削除します。処理されないパラメータは、$Except 配列プロパティを通じて設定できます。

TrustProxies ミドルウェア

ソース ファイル:

app\Http\Middleware\TrustProxies.php

<pre class="brush:php;toolbar:false">&lt;?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; }</pre>関数:

信頼できるプロキシを構成します。信頼できるプロキシのリストは $proxies プロパティで設定でき、$headers プロパティはプロキシの検出に使用される HTTP ヘッダー フィールドを設定します。

VerifyCsrfToken ミドルウェア

ソース ファイル:

app\Http\Middleware\VerifyCsrfToken.php

<pre class="brush:php;toolbar:false">&lt;?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 = [ // ]; }</pre>関数:

リクエスト内のトークンがセッションに保存されているトークンと一致することを確認します。 CSRF 検証を実行しない URL は、$excel 配列属性を通じて設定できます。

関連する推奨事項:
最新の 5 つの Laravel ビデオ チュートリアル

以上がLaravelミドルウェアとは何ですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。