路由級(jí)別的中間件在返回視圖時(shí)應(yīng)準(zhǔn)備響應(yīng)

今天遇到了一個(gè)小問(wèn)題但是為了解決用了很久的時(shí)間,覺(jué)得有必要記錄一下這個(gè)過(guò)程。


首先是設(shè)置了一個(gè)用于身份驗(yàn)證中間件Admin,代碼如下:

public function handle($request, Closure $next)    {
        if(session('user')->user_group != 1){
            $msg ="對(duì)不起,您無(wú)權(quán)訪問(wèn)訪問(wèn)本頁(yè)面!";
            return view('error',compact('msg'));
       }
        return $next($request);
    }

如果使用管理員身份登錄則一切正常,但是如果使用非管理員身份登錄則會(huì)出現(xiàn)一個(gè)錯(cuò)誤。


錯(cuò)誤信息

搜索了很久終于在Laracasts發(fā)現(xiàn)有人有人針對(duì)這個(gè)錯(cuò)誤提問(wèn),其中一個(gè)回答是

There are two types of middleware one that are applied to the global level and one that we apply to the route level. The route level middleware run inside the router's own local middleware stack. When we return some view from controller i.ereturn view('auth/login') the router's method dispatchToRoute explicitly prepares the Response object so that it can be used inside the global middleware (or in general can be returned to the browser). That said, only returning a view never prepares the response so the route lever middlewares throw such errors. you can fix it by simply returning a response rather then a rendered view.

由于英語(yǔ)水平不行并沒(méi)有完全看懂,個(gè)人理解是Laravel有兩種級(jí)別的中間件,一種是應(yīng)用于全局級(jí)別的中間件一種是應(yīng)用于路由級(jí)別的中間件,在只返回一個(gè)視圖時(shí)不會(huì)準(zhǔn)備響應(yīng),所以頭信息為空,導(dǎo)致setCookie()無(wú)法調(diào)用。要修復(fù)這個(gè)問(wèn)題應(yīng)該返回一個(gè)Response對(duì)象,使用該類的view()方法來(lái)取代直接返回一個(gè)渲染視圖。

//replace your `view returns` with `reponse returns` e.g:
return view('home.index')
//replace with
return response()->view('home.index')

而另一個(gè)回答則建議更好的處理方式是自己在中間件里準(zhǔn)備響應(yīng)

use Illuminate\Http\Response;
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
.. 
public function handle($request, Closure $next) {
 $response = $this->prepareResponse($request, $next($request)); 
if ($this->isReading($request) || $this->tokensMatch($request)) {
 return $this->addCookieToResponse($request, $response);
 } 
throw new TokenMismatchException; 
} 
// this one is in fact a copy-paste from the Router 
protected function prepareResponse($request, $response) {
 if ( ! $response instanceof SymfonyResponse) {
 $response = new Response($response); 
} 
return $response->prepare($request); 
}

[原文鏈接][1] 以后有時(shí)間再深入研究
[1]:https://laracasts.com/discuss/channels/general-discussion/disable-global-verifycsrftoken-and-use-as-middleware-doesnt-work-setcookie-on-null

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,259評(píng)論 0 23
  • 應(yīng)用對(duì)象 app.set(name, value):用于設(shè)置Express配置中的環(huán)境變量 Assigns set...
    不系流年系乾坤閱讀 480評(píng)論 0 0
  • 好幾次,我都苦惱,本來(lái)很好的兩個(gè)人。突然間為了一點(diǎn)小小的利益,就可以隨意的誹謗陷害,一般情況下,我都覺(jué)得是別人有負(fù)...
    起舞一笑閱讀 173評(píng)論 0 0
  • 現(xiàn)在會(huì)被各種信息提醒:時(shí)間流逝多快、歲月如何偷偷溜走、2017年已經(jīng)過(guò)了三分之一之類的種種。不得不逼蕓蕓眾生更早的...
    陶柔克閱讀 363評(píng)論 0 0
  • 星期二,上海,霧霾嚴(yán)重 睡到上午10點(diǎn),沖好咖啡,拿出昨天做失敗的曲奇餅,下載簡(jiǎn)書(shū),取名微涼札記,廣播中放著101...
    微涼札記閱讀 203評(píng)論 0 0

友情鏈接更多精彩內(nèi)容