久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何設(shè)置 Laravel 中間件的執(zhí)行順序?

How to set the Laravel middleware order of execution?(如何設(shè)置 Laravel 中間件的執(zhí)行順序?)
本文介紹了如何設(shè)置 Laravel 中間件的執(zhí)行順序?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

Laravel 5 文檔 描述了兩種分配中間件的方法:

  1. 將中間件分配給控制器的路由.
  2. 在控制器的構(gòu)造函數(shù)中指定中間件.

然而,我意識(shí)到在控制器__construct() 函數(shù)中編寫(xiě)的任何代碼都會(huì)在中間件之前運(yùn)行,即使中間件是在控制器的 __construct 函數(shù)的第一行聲明.

However, I realised that any code written in the controllers __construct() function will run before the Middleware, even if the Middleware is declared on the first line of the controller's __construct function.

我在 Laravel github 存儲(chǔ)庫(kù)中找到了一個(gè)關(guān)于類(lèi)似問(wèn)題的錯(cuò)誤報(bào)告.然而,一位合作者結(jié)束了這個(gè)問(wèn)題,指出這是預(yù)期的行為.".

I found a bug report for a similar issue in the Laravel github repository. However a collaborator closed the issue stating "This is the expected behaviour.".

我認(rèn)為 middleware 應(yīng)該是應(yīng)用程序之外的層",而 __construct 函數(shù)是應(yīng)用程序的一部分.

I am thinking that middleware should be "layers" outside the application, while the __construct function is part of the application.

為什么 __construct 函數(shù)在中間件之前執(zhí)行(假設(shè)它是在中間件運(yùn)行之前聲明的)?為什么這是預(yù)期的?

Why is the __construct function executed before the middleware (given it is declared before middleware runs)? and why this is expected?

推薦答案

應(yīng)用程序邏輯駐留在控制器的方法中.所以基本上應(yīng)用程序存在于控制器的方法中,而不是整個(gè)控制器本身.

The application logic resides in the controller's methods. So basically application lives in the controller's methods, not in the whole controller itself.

中間件在請(qǐng)求進(jìn)入相應(yīng)的控制器方法之前運(yùn)行.因此,這始終在實(shí)際應(yīng)用程序之外.除非所有中間件都通過(guò)請(qǐng)求,否則不會(huì)執(zhí)行任何控制器方法.

Middleware runs BEFORE the request enters the respective controller method. And thus, this is always OUTSIDE the real application. No controller method is executed unless all the Middlewares are passing the request.

您放入控制器構(gòu)造函數(shù)中的 $this->middleware("MyMiddleware"); 語(yǔ)句,注冊(cè) MyMiddleware 以在執(zhí)行之前進(jìn)行檢查請(qǐng)求進(jìn)入應(yīng)用程序.

The $this->middleware("MyMiddleware"); statements that you put in the controller constructor, REGISTERS the MyMiddleware for checking before the request enters the application.

如果你看到一個(gè)中間件的代碼并且如果請(qǐng)求通過(guò),則我們使用 $next($request); 語(yǔ)句將其發(fā)送到下一個(gè)中??間件.這允許為單個(gè)請(qǐng)求執(zhí)行多個(gè)中間件.現(xiàn)在,如果 Laravel 直接在 $this->middleware(...); 語(yǔ)句處運(yùn)行中間件,Laravel 可能無(wú)法知道接下來(lái)應(yīng)該檢查哪個(gè)中間件.

If you see the code of a middleware and if the request is passing, then we send it to the next middleware using the $next($request); statement. This allows multiple middlewares to be executed for a single request. Now, if Laravel run the middleware right at the $this->middleware(...); statement, Laravel would probably not be able to know which middleware should be next checked.

所以,Laravel 通過(guò)先注冊(cè)所有中間件,然后將請(qǐng)求一個(gè)一個(gè)地傳遞給所有中間件來(lái)解決這個(gè)問(wèn)題.

So, Laravel solves this by registering all the middlewares first, then passing the request through all the middlewares one by one.

這篇關(guān)于如何設(shè)置 Laravel 中間件的執(zhí)行順序?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Magento products by categories(按類(lèi)別劃分的 Magento 產(chǎn)品)
Resource interpreted as image but transferred with MIME type text/html - Magento(資源被解釋為圖像但使用 MIME 類(lèi)型 text/html 傳輸 - Magento)
Is there an event for customer account registration in Magento?(Magento 中是否有客戶(hù)帳戶(hù)注冊(cè)事件?)
Magento addFieldToFilter: Two fields, match as OR, not AND(Magento addFieldToFilter:兩個(gè)字段,匹配為 OR,而不是 AND)
quot;Error 404 Not Foundquot; in Magento Admin Login Page(“未找到錯(cuò)誤 404在 Magento 管理員登錄頁(yè)面)
Get Order Increment Id in Magento(在 Magento 中獲取訂單增量 ID)
主站蜘蛛池模板: 中国一级大毛片 | 欧美一级毛片免费观看 | 久久久国产一区二区三区四区小说 | 国产一二三区精品视频 | 精品综合久久久 | 999精品视频在线观看 | 免费国产视频在线观看 | 久久成人一区 | 色综合九九 | 免费在线观看av片 | www.黄色在线观看 | 亚洲永久免费观看 | 欧美精品一区在线发布 | 99热播精品 | 久久午夜精品 | 婷婷久久五月 | 91久久国产综合久久 | 国产一级片91 | 91色视频在线观看 | 国产成人精品一区二区三区在线观看 | 成人在线一区二区 | 91 中文字幕 | 日韩精品视频中文字幕 | 国产精品久久久久久婷婷天堂 | 亚洲成人精选 | 欧美第一区 | 国产自产c区 | 日韩成人免费中文字幕 | 男女免费视频网站 | 狠狠干天天干 | 日韩成年人视频在线 | 伊人久久国产 | 欧洲视频一区 | 亚洲精品一区二区三区蜜桃久 | 久久久精彩视频 | 日韩一级在线 | 999免费视频 | 一本综合久久 | 在线观看av网站 | 欧美偷偷操 | 午夜精品久久 |