成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Laravel學習筆記之Middleware源碼解析

開發 前端
本文主要學習Laravel的Middleware的源碼設計思想,并將學習心得分享出來,希望對別人有所幫助。Laravel學習筆記之Decorator Pattern已經聊過Laravel使用了Decorator Pattern來設計Middleware,看Laravel源碼發現其巧妙用了Closure和PHP的一些數組函數來設計Middleware。

說明:本文主要學習Laravel的Middleware的源碼設計思想,并將學習心得分享出來,希望對別人有所幫助。Laravel學習筆記之Decorator Pattern已經聊過Laravel使用了Decorator Pattern來設計Middleware,看Laravel源碼發現其巧妙用了Closure和PHP的一些數組函數來設計Middleware。

開發環境:Laravel5.3 + PHP7 + OS X 10.11

PHP內置函數array_reverse、array_reduce、call_user_func和call_user_func_array

看Laravel源碼之前,先看下這幾個PHP內置函數的使用。首先array_reverse()函數比較簡單,倒置數組,看測試代碼:

  1. $pipes = [ 
  2.     'Pipe1'
  3.     'Pipe2'
  4.     'Pipe3'
  5.     'Pipe4'
  6.     'Pipe5'
  7.     'Pipe6'
  8. ]; 
  9.  
  10. $pipes = array_reverse($pipes); 
  11.  
  12. var_dump($pipes); 
  13.  
  14. // output 
  15. array(6) { 
  16.   [0] => 
  17.   string(5) "Pipe6" 
  18.   [1] => 
  19.   string(5) "Pipe5" 
  20.   [2] => 
  21.   string(5) "Pipe4" 
  22.   [3] => 
  23.   string(5) "Pipe3" 
  24.   [4] => 
  25.   string(5) "Pipe2" 
  26.   [5] => 
  27.   string(5) "Pipe1" 
  28.  

array_reduce內置函數主要是用回調函數去迭代數組中每一個值,并且每一次回調得到的結果值作為下一次回調的初始值,***返回最終迭代的值: 

  1. /** 
  2.  * @link http://php.net/manual/zh/function.array-reduce.php 
  3.  * @param int $v 
  4.  * @param int $w 
  5.  * 
  6.  * @return int 
  7.  */ 
  8. function rsum($v, $w) 
  9.     $v += $w; 
  10.     return $v; 
  11.  
  12. $a = [1, 2, 3, 4, 5]; 
  13. // 10為初始值 
  14. $b = array_reduce($a, "rsum", 10); 
  15. // ***輸出 (((((10 + 1) + 2) + 3) + 4) + 5) = 25 
  16. echo $b . PHP_EOL;   

call_user_func()是執行回調函數,并可輸入參數作為回調函數的參數,看測試代碼: 

  1. class TestCallUserFunc 
  2.     public function index($request) 
  3.     { 
  4.         echo $request . PHP_EOL; 
  5.     } 
  6. }    
  7.  
  8. /** 
  9.  * @param $test 
  10.  */ 
  11. function testCallUserFunc($test) 
  12.     echo $test . PHP_EOL; 
  13.  
  14. // [$class, $method] 
  15. call_user_func(['TestCallUserFunc''index'], 'pipes'); // 輸出'pipes' 
  16.  
  17. // Closure 
  18. call_user_func(function ($passable) { 
  19.     echo $passable . PHP_EOL; 
  20. }, 'pipes'); // 輸出'pipes' 
  21.  
  22. // function 
  23. call_user_func('testCallUserFunc' , 'pipes'); // 輸出'pipes'  

call_user_func_array與call_user_func基本一樣,只不過傳入的參數是數組: 

  1. class TestCallUserFuncArray 
  2.     public function index($request) 
  3.     { 
  4.         echo $request . PHP_EOL; 
  5.     } 
  6.  
  7. /** 
  8.  * @param $test 
  9.  */ 
  10. function testCallUserFuncArray($test) 
  11.     echo $test . PHP_EOL; 
  12.  
  13. // [$class, $method] 
  14. call_user_func_array(['TestCallUserFuncArray''index'], ['pipes']); // 輸出'pipes' 
  15.  
  16. // Closure 
  17. call_user_func_array(function ($passable) { 
  18.     echo $passable . PHP_EOL; 
  19. }, ['pipes']); // 輸出'pipes' 
  20.  
  21. // function 
  22. call_user_func_array('testCallUserFuncArray' , ['pipes']); // 輸出'pipes'  

Middleware源碼解析

了解了幾個PHP內置函數后再去看下Middleware源碼就比較簡單了。Laravel學習筆記之IoC Container實例化源碼解析已經聊過Application的實例化,得到index.php中的$app變量,即\Illuminate\Foundation\Application的實例化對象。然后繼續看下index.php的源碼: 

  1. /** 
  2.  * @var \App\Http\Kernel $kernel 
  3.  */ 
  4. $kernel = $app->make(Illuminate\Contracts\Http\Kernel::class); 
  5.  
  6. $response = $kernel->handle( 
  7.     $request = Illuminate\Http\Request::capture() 
  8. ); 
  9.  
  10. $response->send(); 
  11.  
  12. $kernel->terminate($request, $response);  

首先從容器中解析出Kernel對象,對于\App\Http\Kernel對象的依賴:\Illuminate\Foundation\Application和\Illuminate\Routing\Router,容器會自動解析。看下Kernel的構造函數: 

  1. /** 
  2.      * Create a new HTTP kernel instance. 
  3.      * 
  4.      * @param  \Illuminate\Contracts\Foundation\Application  $app 
  5.      * @param  \Illuminate\Routing\Router  $router 
  6.      */ 
  7.     public function __construct(Application $app, Router $router) 
  8.     { 
  9.         $this->app    = $app; 
  10.         $this->router = $router; 
  11.  
  12.         foreach ($this->middlewareGroups as $key => $middleware) { 
  13.             $router->middlewareGroup($key, $middleware); 
  14.         } 
  15.  
  16.         foreach ($this->routeMiddleware as $key => $middleware) { 
  17.             $router->middleware($key, $middleware); 
  18.         } 
  19.     } 
  20.      
  21.     // \Illuminate\Routing\Router內的方法 
  22.     public function middlewareGroup($name, array $middleware) 
  23.     { 
  24.         $this->middlewareGroups[$name] = $middleware; 
  25.  
  26.         return $this; 
  27.     } 
  28.      
  29.     public function middleware($name, $class) 
  30.     { 
  31.         $this->middleware[$name] = $class; 
  32.  
  33.         return $this; 
  34.     }  

構造函數初始化了幾個中間件數組,$middleware[ ], $middlewareGroups[ ]和$routeMiddleware[ ],Laravel5.0的時候記得中間件數組還沒有分的這么細。然后就是Request的實例化: 

  1. $request = Illuminate\Http\Request::capture() 

這個過程以后再聊吧,不管咋樣,得到了Illuminate\Http\Request對象,然后傳入Kernel中: 

  1. /** 
  2.     * Handle an incoming HTTP request. 
  3.     * 
  4.     * @param  \Illuminate\Http\Request  $request 
  5.     * @return \Illuminate\Http\Response 
  6.     */ 
  7.    public function handle($request) 
  8.    { 
  9.        try { 
  10.            $request->enableHttpMethodParameterOverride(); 
  11.  
  12.            $response = $this->sendRequestThroughRouter($request); 
  13.        } catch (Exception $e) { 
  14.            $this->reportException($e); 
  15.  
  16.            $response = $this->renderException($request, $e); 
  17.        } catch (Throwable $e) { 
  18.            $this->reportException($e = new FatalThrowableError($e)); 
  19.  
  20.            $response = $this->renderException($request, $e); 
  21.        } 
  22.  
  23.        $this->app['events']->fire('kernel.handled', [$request, $response]); 
  24.  
  25.        return $response; 
  26.    }  

主要是sendRequestThroughRouter($request)函數執行了轉換操作:把\Illuminate\Http\Request對象轉換成了\Illuminate\Http\Response,然后通過Kernel的send()方法發送給客戶端。同時,順便觸發了kernel.handled內核已處理請求事件。OK,重點關注下sendRequestThroughRouter($request)方法: 

  1. /** 
  2.      * Send the given request through the middleware / router. 
  3.      * 
  4.      * @param  \Illuminate\Http\Request  $request 
  5.      * @return \Illuminate\Http\Response 
  6.      */ 
  7.     protected function sendRequestThroughRouter($request) 
  8.     { 
  9.         $this->app->instance('request', $request); 
  10.  
  11.         Facade::clearResolvedInstance('request'); 
  12.  
  13.         /* 依次執行$bootstrappers中每一個bootstrapper的bootstrap()函數,做了幾件準備事情: 
  14.         1. 環境檢測 
  15.         2. 配置加載 
  16.         3. 日志配置 
  17.         4. 異常處理 
  18.         5. 注冊Facades 
  19.         6. 注冊Providers 
  20.         7. 啟動服務 
  21.          protected $bootstrappers = [ 
  22.             'Illuminate\Foundation\Bootstrap\DetectEnvironment'
  23.             'Illuminate\Foundation\Bootstrap\LoadConfiguration'
  24.             'Illuminate\Foundation\Bootstrap\ConfigureLogging'
  25.             'Illuminate\Foundation\Bootstrap\HandleExceptions'
  26.             'Illuminate\Foundation\Bootstrap\RegisterFacades'
  27.             'Illuminate\Foundation\Bootstrap\RegisterProviders'
  28.             'Illuminate\Foundation\Bootstrap\BootProviders'
  29.         ];*/ 
  30.         $this->bootstrap(); 
  31.  
  32.         return (new Pipeline($this->app)) 
  33.                     ->send($request) 
  34.                     ->through($this->app->shouldSkipMiddleware() ? [] : $this->middleware) 
  35.                     ->then($this->dispatchToRouter()); 
  36.     }  

$this->bootstrap()主要是做了程序初始化工作,以后再聊具體細節。然后是Pipeline來傳輸Request,Laravel中把Pipeline管道單獨拿出來作為一個service(可看Illuminate/Pipeline文件夾),說明Pipeline做的事情還是很重要的:主要就是作為Request的傳輸管道,依次通過$middlewares[ ], 或middlewareGroups[ ], 或$routeMiddleware[ ]這些中間件的前置操作,和控制器的某個action或者直接閉包處理得到Response,然后又帶著Reponse依次通過$middlewares[ ], 或middlewareGroups[ ], 或$routeMiddleware[ ]這些中間件的后置操作得到準備就緒的Response,然后通過send()發送給客戶端。

這個過程有點像汽車工廠的生產一樣,Pipeline是傳送帶,起初Request可能就是個汽車空殼子,經過傳送帶旁邊的一個個機械手middleware@before的過濾和操作(如檢查零件剛度是不是合格,殼子尺寸是不是符合要求,給殼子噴個漆或抹個油啥的),然后進入中央控制區加個發動機(Controller@action,或Closure),然后又繼續經過檢查和附加操作middleware@after(如添加個擋風鏡啥的),然后通過門外等著的火車直接運送到消費者手里send()。在每一步裝配過程中,都需要Service來支持,Service是通過Container來解析{make()}提供的,并且Service是通過ServiceProvider注冊綁定{bind(),singleton(),instance()}到Container中的。

看下Pipeline的send()和through()源碼: 

  1. public function send($passable) 
  2.    { 
  3.        $this->passable = $passable; 
  4.  
  5.        return $this; 
  6.    } 
  7.     
  8.    public function through($pipes) 
  9.    { 
  10.        $this->pipes = is_array($pipes) ? $pipes : func_get_args(); 
  11.  
  12.        return $this; 
  13.    }  

send()傳送的對象是Request,through()所要通過的對象是$middleware[ ],OK,再看下dispatchToRouter()的源碼直接返回一個Closure: 

  1. protected function dispatchToRouter() 
  2.     { 
  3.         return function ($request) { 
  4.             $this->app->instance('request', $request); 
  5.  
  6.             return $this->router->dispatch($request); 
  7.         }; 
  8.     } 

 然后重點看下then()函數源碼: 

  1. public function then(Closure $destination) 
  2.     { 
  3.         $firstSlice = $this->getInitialSlice($destination); 
  4.  
  5.         $pipes = array_reverse($this->pipes); 
  6.  
  7.         // $this->passable = Request對象 
  8.         return call_user_func( 
  9.             array_reduce($pipes, $this->getSlice(), $firstSlice), $this->passable 
  10.         ); 
  11.     } 
  12.      
  13.     protected function getInitialSlice(Closure $destination) 
  14.     { 
  15.         return function ($passable) use ($destination) { 
  16.             return call_user_func($destination, $passable); 
  17.         }; 
  18.     } 

 這里假設$middlewares為(盡管源碼中$middlewares只有一個CheckForMaintenanceMode::class): 

  1. $middlewares = [ 
  2.     CheckForMaintenanceMode::class, 
  3.     AddQueuedCookiesToResponse::class, 
  4.     StartSession::class, 
  5.     ShareErrorsFromSession::class, 
  6.     VerifyCsrfToken::class, 
  7. ]; 

 先獲得***個slice(這里作者是比作'洋蔥',一層層的穿過,從一側穿過到另一側,比喻倒也形象)并作為array_reduce()的初始值,就像上文中array_reduce()測試例子中的10這個初始值,這個初始值現在是個閉包: 

  1. $destination = function ($request) { 
  2.     $this->app->instance('request', $request); 
  3.     return $this->router->dispatch($request); 
  4. }; 
  5.  
  6. $firstSlice = function ($passable) use ($destination) { 
  7.     return call_user_func($destination, $passable); 
  8. }; 

 OK,然后要對$middlewares[ ]進行翻轉,為啥要翻轉呢?

看過這篇Laravel學習筆記之Decorator Pattern文章就會發現,在Client類利用Decorator Pattern進行依次裝飾的時候,是按照$middlewares[ ]數組中值倒著new的: 

  1. public function wrapDecorator(IMiddleware $decorator) 
  2.    { 
  3.        $decorator = new VerifyCsrfToken($decorator); 
  4.        $decorator = new ShareErrorsFromSession($decorator); 
  5.        $decorator = new StartSession($decorator); 
  6.        $decorator = new AddQueuedCookiesToResponse($decorator); 
  7.        $response  = new CheckForMaintenanceMode($decorator); 
  8.  
  9.        return $response; 
  10.    } 

 這樣才能得到一個符合$middlewares[ ]順序的$response對象: 

  1. $response = new CheckForMaintenanceMode( 
  2.                 new AddQueuedCookiesToResponse( 
  3.                     new StartSession( 
  4.                         new ShareErrorsFromSession( 
  5.                             new VerifyCsrfToken( 
  6.                                 new Request() 
  7.                         ) 
  8.                     ) 
  9.                 ) 
  10.             ) 
  11.         ); 

 看下array_reduce()中的迭代回調函數getSlice(){這個迭代回調函數比作剝洋蔥時獲取每一層洋蔥slice,初始值是$firstSlice}: 

  1. protected function getSlice() 
  2.     { 
  3.         return function ($stack, $pipe) { 
  4.             return function ($passable) use ($stack, $pipe) { 
  5.                 if ($pipe instanceof Closure) { 
  6.                     return call_user_func($pipe, $passable, $stack); 
  7.                 } elseif (! is_object($pipe)) { 
  8.                     list($name, $parameters) = $this->parsePipeString($pipe); 
  9.                     $pipe = $this->container->make($name); 
  10.                     $parameters = array_merge([$passable, $stack], $parameters); 
  11.                 } else
  12.                     $parameters = [$passable, $stack]; 
  13.                 } 
  14.  
  15.                 return call_user_func_array([$pipe, $this->method], $parameters); 
  16.             }; 
  17.         }; 
  18.     } 

 返回的是個閉包,仔細看下第二層閉包里的邏輯,這里$middlewares[ ]傳入的是每一個中間件的名字,然后通過容器解析出每一個中間件對象: 

  1. $pipe = $this->container->make($name); 

并***用call_user_func_array([$class, $method], array $parameters)來調用這個$class里的$method方法,參數是$parameters。

Demo

接下來寫個demo看下整個流程。先簡化下getSlice()函數,這里就默認$pipe傳入的是類名稱(整個demo中所有class都在同一個文件內):

 

  1. // PipelineTest.php 
  2.  
  3. // Get the slice in every step. 
  4. function getSlice() 
  5.     return function ($stack, $pipe) { 
  6.         return function ($passable) use ($stack, $pipe) { 
  7.             /** 
  8.              * @var Middleware $pipe 
  9.              */ 
  10.             return call_user_func_array([$pipe, 'handle'], [$passable, $stack]); 
  11.         }; 
  12.     }; 
  13.  

再把$middlewares[ ]中五個中間件類寫上,對于前置操作和后置操作做個簡化,直接echo字符串:

    1.  // PipelineTest.php 
    2. <?php 
    3.  
    4. interface Middleware 
    5.     public static function handle($request, Closure $closure); 
    6.  
    7. class CheckForMaintenanceMode implements Middleware 
    8.     public static function handle($request, Closure $next
    9.     { 
    10.         echo $request . ': Check if the application is in the maintenance status.' . PHP_EOL; 
    11.         $next($request); 
    12.     } 
    13.  
    14. class AddQueuedCookiesToResponse implements Middleware 
    15.     public static function handle($request, Closure $next
    16.     { 
    17.         $next($request); 
    18.         echo $request . ': Add queued cookies to the response.' . PHP_EOL; 
    19.     } 
    20.  
    21. class StartSession implements Middleware 
    22.     public static function handle($request, Closure $next
    23.     { 
    24.         echo $request . ': Start session of this request.' . PHP_EOL; 
    25.         $next($request); 
    26.         echo $request . ': Close session of this response.' . PHP_EOL; 
    27.     } 
    28.  
    29. class ShareErrorsFromSession implements Middleware 
    30.     public static function handle($request, Closure $next
    31.     { 
    32.         $next($request); 
    33.         echo $request . ': Share the errors variable from response to the views.' . PHP_EOL; 
    34.     } 
    35.  
    36. class VerifyCsrfToken implements Middleware 
    37.     public static function handle($request, Closure $next
    38.     { 
    39.         echo $request . ': Verify csrf token when post request.' . PHP_EOL; 
    40.         $next($request); 
    41.     } 
    42.  

給上完整的一個Pipeline類,這里的Pipeline對Laravel中的Pipeline做了稍微簡化,只選了幾個重要的函數: 

  1. // PipelineTest.php 
  2.  
  3. class Pipeline  
  4.     /** 
  5.      * @var array 
  6.      */ 
  7.     protected $middlewares = []; 
  8.  
  9.     /** 
  10.      * @var int 
  11.      */ 
  12.     protected $request; 
  13.  
  14.     // Get the initial slice 
  15.     function getInitialSlice(Closure $destination) 
  16.     { 
  17.         return function ($passable) use ($destination) { 
  18.             return call_user_func($destination, $passable); 
  19.         }; 
  20.     } 
  21.      
  22.     // Get the slice in every step. 
  23.     function getSlice() 
  24.     { 
  25.         return function ($stack, $pipe) { 
  26.             return function ($passable) use ($stack, $pipe) { 
  27.                 /** 
  28.                  * @var Middleware $pipe 
  29.                  */ 
  30.                 return call_user_func_array([$pipe, 'handle'], [$passable, $stack]); 
  31.             }; 
  32.         }; 
  33.     } 
  34.      
  35.     // When process the Closure, send it as parameters. Here, input an int number. 
  36.     function send(int $request) 
  37.     { 
  38.         $this->request = $request; 
  39.         return $this; 
  40.     } 
  41.  
  42.     // Get the middlewares array. 
  43.     function through(array $middlewares) 
  44.     { 
  45.         $this->middlewares = $middlewares; 
  46.         return $this; 
  47.     } 
  48.      
  49.     // Run the Filters. 
  50.     function then(Closure $destination) 
  51.     { 
  52.         $firstSlice = $this->getInitialSlice($destination); 
  53.      
  54.         $pipes = array_reverse($this->middlewares); 
  55.          
  56.         $run = array_reduce($pipes, $this->getSlice(), $firstSlice); 
  57.      
  58.         return call_user_func($run, $this->request); 
  59.     } 

 OK,現在開始傳入Request,這里簡化為一個整數而不是Request對象了: 

  1. // PipelineTest.php 
  2.  
  3. /** 
  4.  * @return \Closure 
  5.  */ 
  6. function dispatchToRouter() 
  7.     return function ($request) { 
  8.         echo $request . ': Send Request to the Kernel, and Return Response.' . PHP_EOL; 
  9.     }; 
  10.  
  11. $request = 10; 
  12.  
  13. $middlewares = [ 
  14.     CheckForMaintenanceMode::class, 
  15.     AddQueuedCookiesToResponse::class, 
  16.     StartSession::class, 
  17.     ShareErrorsFromSession::class, 
  18.     VerifyCsrfToken::class, 
  19. ]; 
  20.  
  21. (new Pipeline())->send($request)->through($middlewares)->then(dispatchToRouter()); 

 執行php PipelineTest.php得到Response: 

  1. 10: Check if the application is in the maintenance status. 
  2. 10: Start session of this request. 
  3. 10: Verify csrf token when post request. 
  4. 10: Send Request to the Kernel, and Return Response. 
  5. 10: Share the errors variable from response to the views. 
  6. 10: Close session of this response. 
  7. 10: Add queued cookies to the response. 

 一步一步分析下執行過程:

1.首先獲取$firstSlice 

  1. $destination = function ($request) { 
  2.     echo $request . ': Send Request to the Kernel, and Return Response.' . PHP_EOL; 
  3. }; 
  4. $firstSlice = function ($passable) use ($destination) { 
  5.     return call_user_func($destination, $passable); 
  6. }; 

 這時經過初始化后: 

  1. $this->request = 10; 
  2. $pipes = [ 
  3.     VerifyCsrfToken::class, 
  4.     ShareErrorsFromSession::class, 
  5.     StartSession::class, 
  6.     AddQueuedCookiesToResponse::class, 
  7.     CheckForMaintenanceMode::class, 
  8. ]; 

 2.執行***次getSlice()后的結果作為新的$stack,其值為: 

  1. $stack   = $firstSlice; 
  2. $pipe    = VerifyCsrfToken::class; 
  3. $stack_1 = function ($passable) use ($stack, $pipe) { 
  4.         /** 
  5.         * @var Middleware $pipe 
  6.         */             
  7.     return call_user_func_array([$pipe, 'handle'], [$passable, $stack]); 
  8. };  

3.執行第二次getSlice()后的結果作為新的$stack,其值為: 

  1. $stack   = $stack_1; 
  2. $pipe    = ShareErrorsFromSession::class; 
  3. $stack_2 = function ($passable) use ($stack, $pipe) { 
  4.         /** 
  5.         * @var Middleware $pipe 
  6.         */             
  7.     return call_user_func_array([$pipe, 'handle'], [$passable, $stack]); 
  8. };  

4.執行第三次getSlice()后的結果作為新的$stack,其值為: 

  1. $stack   = $stack_2; 
  2. $pipe    = StartSession::class; 
  3. $stack_3 = function ($passable) use ($stack, $pipe) { 
  4.         /** 
  5.         * @var Middleware $pipe 
  6.         */             
  7.     return call_user_func_array([$pipe, 'handle'], [$passable, $stack]); 
  8. };  

5.執行第四次getSlice()后的結果作為新的$stack,其值為: 

  1. $stack   = $stack_3; 
  2. $pipe    = AddQueuedCookiesToResponse::class; 
  3. $stack_4 = function ($passable) use ($stack, $pipe) { 
  4.         /** 
  5.         * @var Middleware $pipe 
  6.         */             
  7.     return call_user_func_array([$pipe, 'handle'], [$passable, $stack]); 
  8. };  

6.執行第五次getSlice()后的結果作為新的$stack,其值為: 

  1. $stack   = $stack_4; 
  2. $pipe    = CheckForMaintenanceMode::class; 
  3. $stack_5 = function ($passable) use ($stack, $pipe) { 
  4.         /** 
  5.         * @var Middleware $pipe 
  6.         */             
  7.     return call_user_func_array([$pipe, 'handle'], [$passable, $stack]); 
  8. };  

這時,$stack_5也就是then()里的$run,然后執行call_user_func($run, 10),看執行過程:

1.$stack_5(10) = CheckForMaintenanceMode::handle(10, $stack_4) 

  1. echo '10: Check if the application is in the maintenance status.' . PHP_EOL; 
  2. stack_4(10); 

 2.$stack_4(10) = AddQueuedCookiesToResponse::handle(10, $stack_3) 

  1. $stack_3(10); 
  2.  
  3. echo '10: Add queued cookies to the response.' . PHP_EOL;  

3.$stack_3(10) = StartSession::handle(10, $stack_2) 

  1. echo '10: Start session of this request.' . PHP_EOL; 
  2.  
  3. $stack_2(10); 
  4. echo '10: Close session of this response.' . PHP_EOL;

 4.$stack_2(10) = ShareErrorsFromSession::handle(10, $stack_1) 

  1. $stack_1(10); 
  2.  
  3. echo '10: Share the errors variable from response to the views.' . PHP_EOL;  

5.$stack_1(10) = VerifyCsrfToken::handle(10, $firstSlice) 

  1. echo '10: Verify csrf token when post request.' . PHP_EOL; 
  2.  
  3. $firstSlice(10); 

 6.$firstSlice(10) = 

  1. $firstSlice(10) = call_user_func($destination, 10) = echo '10: Send Request to the Kernel, and Return Response.' . PHP_EOL; 

OK,再把上面執行順序整理一下: 

  1. 1. echo '10: Check if the application is in the maintenance status.' . PHP_EOL; // ***個step 
  2.  
  3. 3_1. echo '10: Start session of this request.' . PHP_EOL; // 第三個step 
  4.  
  5. 5. echo '10: Verify csrf token when post request.' . PHP_EOL; // 第五個step 
  6.  
  7. 6.echo '10: Send Request to the Kernel, and Return Response.' . PHP_EOL; //第六個step 
  8.  
  9. 4. echo '10: Share the errors variable from response to the views.' . PHP_EOL; // 第四個step 
  10.  
  11. 3_2. echo '10: Close session of this response.' . PHP_EOL; // 第三個step 
  12.  
  13. 2. echo '10: Add queued cookies to the response.' . PHP_EOL; // 第二個step  

經過上面的一步步分析,就能很清楚Laravel源碼中Middleware的執行步驟了。再復雜的步驟只要一步步拆解,就很清晰每一步的邏輯,然后把步驟組裝,就能知道全貌了。

總結:本文主要學習了Laravel的Middleware的源碼,學習完后就知道沒有什么神秘之處,只需要動手一步步拆解就行。后面再學習下Container的源碼,到時見。

責任編輯:龐桂玉 來源: segmentfault
相關推薦

2016-09-20 10:15:49

LaravelPHPContainer

2016-09-21 21:49:37

PromiseJavascript前端

2021-02-20 06:09:46

libtask協程鎖機制

2011-04-22 14:14:21

MySQL偷窺線程

2010-07-27 15:42:18

AdobeFlex

2010-06-12 13:08:51

UML全稱

2022-02-14 14:47:11

SystemUIOpenHarmon鴻蒙

2022-12-07 08:02:43

Spring流程IOC

2016-12-15 09:44:31

框架Caffe源碼

2010-06-13 12:49:23

UML及建模

2011-03-08 16:30:24

Proftpd

2011-03-08 16:30:40

Proftpd

2010-06-28 15:41:17

UML圖類型

2010-06-28 18:44:54

UML對象圖

2011-09-01 14:14:00

jQuery Mobi

2022-08-08 08:03:44

MySQL數據庫CBO

2022-05-17 10:42:36

reboot源碼解析

2017-06-07 14:58:39

Redis源碼學習Redis事務

2012-02-23 11:06:18

JavaPlay FramewPlay!

2010-06-28 18:36:06

UML協作圖
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 午夜精品导航 | 青草青草久热精品视频在线观看 | 精品美女| 国产成人自拍一区 | 欧美日韩一区不卡 | 亚洲国产精品久久久久秋霞不卡 | 337p日本欧洲亚洲大胆精蜜臀 | 日韩久久精品视频 | 亚洲电影免费 | 在线国产视频 | 国产一区亚洲二区三区 | 日韩成人影院在线观看 | 婷婷色成人 | 最新国产精品精品视频 | 国产一区二区欧美 | www.99热| 久久亚洲精品久久国产一区二区 | 国产精品极品美女在线观看免费 | 免费在线观看黄视频 | 午夜成人免费视频 | 久久成人精品视频 | 很黄很污的网站 | 亚洲一区二区三区免费在线观看 | 97精品国产97久久久久久免费 | 亚欧洲精品在线视频免费观看 | 深爱激情综合 | 免费国产视频在线观看 | 伊人网影院 | 国产精品久久久久久久久久久久久 | 欧美在线天堂 | 亚洲永久字幕 | 亚洲 欧美 日韩 精品 | 日韩一级免费电影 | 欧美一级二级在线观看 | www.操.com| 欧美 日韩 综合 | 久久国产日本 | 亚洲 成人 在线 | 毛片一区 | 国产视频第一页 | 亚洲精品中文字幕在线观看 |