這篇文章給大家分享的是有關(guān)laravel有什么中間件的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

瀘水網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián),瀘水網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為瀘水成百上千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站建設(shè)要多少錢,請找那個售后服務(wù)好的瀘水做網(wǎng)站的公司定做!
中間件有:1、Authenticate;2、CheckForMaintenanceMode;3、EncryptCookies;4、RedirectIfAuthenticated;5、TrimStrings;6、TrustProxies等等。
本教程操作環(huán)境:windows7系統(tǒng)、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('login');
}
}
}作用:
用戶身份驗證。可修改 redirectTo 方法,返回未經(jīng)身份驗證的用戶應(yīng)該重定向到的路徑。
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 數(shù)組屬性設(shè)置在維護模式下仍能訪問的網(wǎng)址。
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 數(shù)組屬性設(shè)置不做加密處理的 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('/home');
}
return $next($request);
}
}作用:
當請求頁是 注冊、登錄、忘記密碼 時,檢測用戶是否已經(jīng)登錄,如果已經(jīng)登錄,那么就重定向到首頁,如果沒有就打開相應(yīng)界面。可以在 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 = [
'password',
'password_confirmation',
];
}作用:
對請求參數(shù)內(nèi)容進行 前后空白字符清理。可通過 $except 數(shù)組屬性設(shè)置不做處理的參數(shù)。
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 屬性設(shè)置可信代理列表,$headers 屬性設(shè)置用來檢測代理的 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 數(shù)組屬性設(shè)置不做 CSRF 驗證的網(wǎng)址。
感謝各位的閱讀!關(guān)于“l(fā)aravel有什么中間件”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
新聞標題:laravel有什么中間件
文章路徑:http://www.yijiale78.com/article22/pehccc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、網(wǎng)站設(shè)計、虛擬主機、Google、網(wǎng)站制作、網(wǎng)站營銷
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)