<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      Laravel 登錄后跳轉回登錄前瀏覽的頁面

      一、經過 Auth 中間件檢查后跳轉至登錄頁面

      也就是沒有通過 auth 中間件的認證檢查,被 auth 中間件攔截后跳轉至登錄頁面。這種情況下,Laravel 默認會在用戶登錄成功后自動跳轉回登錄前瀏覽的頁面。auth 中間件是怎么做到的?

      打開 auth 中間件文件:

      // vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php
      
      protected function authenticate(array $guards)
      {
          if (empty($guards)) {
                return $this->auth->authenticate();
          }
      
          foreach ($guards as $guard) {
              if ($this->auth->guard($guard)->check()) {
                return $this->auth->shouldUse($guard);
              }
          }
      
          throw new AuthenticationException('Unauthenticated.', $guards);
      }
      

        

      auth 中間件通過 authenticate() 方法檢查用戶是否經過了認證,如果沒有經過認證就拋出一個異常。其中并沒有跳轉至登錄頁面的操作,也就是說 Laravel 是在捕捉到這個異常后進行進一步的操作。

      Laravel 在異常處理類 Illuminate\Foundation\Exceptions\Handler 中處理這個 AuthenticationException 異常。由于在異常處理類的 $internalDontReport 屬性中包含了該異常,所以 AuthenticationException 異常不需要報告或者寫入日志,只需要通過 render() 方法處理為一個響應。在 render() 方法中會調用 unauthenticated() 方法來處理 AuthenticationException 異常

      protected function unauthenticated($request, AuthenticationException $exception)
      {
            return $request->expectsJson()
              ? response()->json(['message' => $exception->getMessage()], 401)
              : redirect()->guest(route('login'));
      }
      

        

      在 unauthenticated() 方法中首先檢查請求是否需要 Json 響應,如果不需要就會執行 redirect()->guest() 方法

      public function guest($path, $status = 302, $headers = [], $secure = null)
      {
          $this->session->put('url.intended', $this->generator->full());
      
          return $this->to($path, $status, $headers, $secure);
      }
      

        

      在重定向的 guest() 方法中,先將用戶訪問(但沒通過認證)的頁面地址存入 Session 的 url.intended 鍵上,然后重定向到登錄界面。這個 Session 的 url.intended 鍵被創建的意義就是在登錄成功后用來跳轉的。

      打開登錄控制器 Auth\LoginController.php 文件中 LoginController 繼承的 AuthenticatesUsers 這個 Trait。在這個 Trait 中,login() 方法處理登錄請求,驗證成功后調用 sendLoginResponse() 方法返回響應:

      // vendor/laravel/framework/src/Illuminate/Foundation/Auth/AuthenticatesUsers.php
      
      protected function sendLoginResponse(Request $request)
      {
          $request->session()->regenerate();
      
          $this->clearLoginAttempts($request);
      
          return $this->authenticated($request, $this->guard()->user()) 
            ?: redirect()->intended($this->redirectPath());
      }
      

        

      在該方法最后的 return 中可以看到:如果 authenticated() 方法返回值不為真,則執行 redirect()->intended() 方法。而 authenticated() 方法默認為空,所以必然會執行 redirect()->intended() 方法

      // vendor/laravel/framework/src/Illuminate/Routing/Redirector.php
      
      public function intended($default = '/', $status = 302, $headers = [], $secure = null)
      {
          $path = $this->session->pull('url.intended', $default);
      
          return $this->to($path, $status, $headers, $secure);
      }
      

        

      在重定向的 intended() 方法中會檢查 Seesion url.intended 鍵的值。如果有值,就會跳轉到該地址,也就是訪問但被 Auth 中間件攔截的那個頁面。

      總結流程如下:

      訪問需要認證的頁面 -> 被 Auth 中間件攔截后拋出異常 -> 處理異常:在 Session 中存入要訪問的頁面地址,然后跳轉至登錄頁面 -> 登錄成功后從 Session 中取出先前存入的頁面地址,并跳轉至該地址。

      二、由不需認證的頁面跳轉至登錄頁面

      也就是訪問的頁面是公開的,登錄或者沒登錄的用戶都能訪問,在這個頁面上點擊登錄按鈕后進入登錄頁面。這種情況下,Laravel 默認返回的是域名的根地址。只要搞明白了第一種情況中 Lararvel 的處理流程,這種情況處理起來非常簡單:

      只需在 Auth\LoginController.php 控制器中重寫其繼承的 AuthenticatesUsers 這個 Trait 中的 showLoginForm() 方法即可:

      // app/Http/Controllers/Auth/LoginController.php
      use AuthenticatesUsers;
      
      // 打開登錄頁面
      public function showLoginForm()
      {
          session(['url.intended'=>url()->previous()]);
      
          return view('auth.login');
      }
      

        

      只需在原有的 showLoginForm() 方法中添加一行即可!這個操作的關鍵就是打開登錄頁面時,將上一個瀏覽的頁面地址存入 Session 的 url.intended 鍵。

      由于登錄步驟和第一種情況一樣,所以 Laravel 會在登錄成功后檢查 Session url.intended 鍵的值,如果有值就會跳轉到該頁面。

      (完)

      posted @ 2019-07-21 00:23  神馬和浮云  閱讀(3241)  評論(1)    收藏  舉報
      主站蜘蛛池模板: 亚洲人成电影在线天堂色| 日本高清在线观看WWW色| 一卡2卡三卡4卡免费网站| 亚洲AV成人片不卡无码| 四虎国产精品免费久久| 亚洲欧美成人久久综合中文网| 亚洲欧美在线一区中文字幕| 成av免费大片黄在线观看| 免费观看的av在线播放| 信丰县| 亚洲国产成人精品女人久| 日韩中文字幕人妻一区| 18禁超污无遮挡无码网址| 国产内射xxxxx在线| 无码AV中文字幕久久专区| 国产午夜精品久久精品电影| 无翼乌口工全彩无遮挡h全彩 | 精品国产人妻一区二区三区久久| 婷婷色香五月综合缴缴情香蕉| 91精品国产免费人成网站| 人妻少妇精品无码专区二区| 乱人伦人妻中文字幕| 精品激情视频一区二区三区| 中文字幕亚洲国产精品| 国产成人久久精品流白浆| 欧美私人情侣网站| 亚洲第一无码AV无码专区| 国产精品永久免费无遮挡| 亚洲成人资源在线观看| 亚洲午夜成人精品电影在线观看| 久久香蕉欧美精品| 亚欧乱色精品免费观看| 日韩大片高清播放器| 久久av高潮av无码av喷吹| 国产精品美女久久久久久麻豆| 久久精品一偷一偷国产| 亚洲精品国产精品国在线| gogogo高清在线播放免费| 亚洲中文字幕久在线| 国产精品一品二区三四区| 国产AV影片麻豆精品传媒|