? ??? ????? Laravel? ???? ?? ??? ?????. ???? ? ??? ??? ??? ???. ??? ??? ??? ????.
#1 ??????
? ??????? ?? ??? ??? ???? ?? ? ?? ???? ???? ?? ??? ?? ??? ??? ?? ? ????. ??? ?? if else? ??? ???? ??, ?? ? ?? ??? ???? ?? ??, ?? ?? ??? ? ????? ??, ??? ?? ???? ???? ???, ????? ? ??? ??? ? ????. ??? ??? ????? ???? ??? ?? ???? ? ????.
#2 Laravel? ????
Laravel?? ????? ??? ??? IlluminatePipelinePipeline ???? ??? ?????. ?? ????? ????? ??? ???????. ?? ?????. ?? ? ??? ???? ??? ?? ?? ???? ???.
public function handle($request, Closure $next) { //do something for $request return $next($request); }
#3 ???? ?? ??
??? ????? ????? Pipeline?? ???? ?? ??? IlluminateRoutingRouter In? ????.
return (new Pipeline($this->container)) ->send($request) ->through($middleware) ->then(function ($request) use ($route) { return $this->prepareResponse( $request, $route->run($request) ); });
???? ?? ???? ? ?? ???? ???? ?? ? ? ????. ? ?? ???? ??? ???????.
send ???
public function send($passable){ $this->passable = $passable; return $this; }
?? send ???? ?? ?? ?? ????. ? ?? ?????? ??????? ?? ??? HTTP ?? ???????.
through method
public function through($pipes){ $this->pipes = is_array($pipes) ? $pipes : func_get_args(); return $this; }
through method ?? ?? ????, ?? ???? ????? ???? ??? ???? ????.
then ???
then ??? ??? ?? ????? ???? ?? ????.
public function then(Closure $destination){ //then方法接受一個(gè)閉包作為參數(shù),然后經(jīng)過(guò)getInitialSlice包裝,而getInitialSlice返回的其實(shí)也是一個(gè)閉包,如果還不知道什么是閉包先去看PHP文檔 $firstSlice = $this->getInitialSlice($destination); //反轉(zhuǎn)中間件數(shù)組,主要是利用了棧的特性,用處接下來(lái)再說(shuō) $pipes = array_reverse($this->pipes); //這個(gè)call_user_func先不要看,它其實(shí)就是執(zhí)行了一個(gè)array_reduce返回的閉包 return call_user_func( //接下來(lái)用array_reduce來(lái)用回調(diào)函數(shù)處理數(shù)組,建議先去PHP文檔讀懂a(chǎn)rray_reduce的執(zhí)行原理。其實(shí)arrary_reduce什么事情都沒(méi)干,就是包裝閉包然后移交給call_user_func來(lái)執(zhí)行 array_reduce($pipes, $this->getSlice(), $firstSlice), $this->passable ); }
??? ? ?? ???? ????. ??? ?? ????? ?? ?????.
aray_reduce? ? ?? ?????? ??? ????? ???? getSlice() ???? ?? ??? ??? ???.
protected function getSlice(){ return function ($stack, $pipe) { //這里$stack return function ($passable) use ($stack, $pipe) { if ($pipe instanceof Closure) { return call_user_func($pipe, $passable, $stack); } else { list($name, $parameters) = $this->parsePipeString($pipe); return call_user_func_array([$this->container->make($name), $this->method], array_merge([$passable, $stack], $parameters)); } }; }; }
??? ????? ?? ?? ????. ??? ??? ?????. ????? ?? getSlice()? ?? A? ???? ?? A? ?? B? ?????. ? ? ?? ??? ?????? ?? ???? ?? ??? ???? ?? $next($request) ? ???? $next($request) ? ???? ????? ?? ???? ??? ? ???? ?? A?? ?? B? ?????. ?? ????.
??? ?? ???? ?????.
//這里的$stack其實(shí)就是閉包,第一次遍歷的時(shí)候會(huì)傳入$firstSlice這個(gè)閉包,以后每次都會(huì)傳入下面的那個(gè)function; 而$pipe就是每一個(gè)中間件 array_reduce($pipes, function ($stack, $pipe) { return function ($passable) use ($stack, $pipe) { }; }, $firstSlice);
? ??? ?? ?????.
//判斷是否為閉包,這里就是判斷中間件形式是不是閉包,是的話直接執(zhí)行并且傳入$passable[請(qǐng)求實(shí)例]和$stack[傳遞給下一個(gè)中間件的閉包],并且返回 if ($pipe instanceof Closure) { return call_user_func($pipe, $passable, $stack); //不是閉包的時(shí)候就是形如這樣Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode執(zhí)行 } else { //解析,把名稱返回,這個(gè)$parameters看了許久源碼還是看不懂,應(yīng)該是和參數(shù)相關(guān),不過(guò)不影響我們的分析 list($name, $parameters) = $this->parsePipeString($pipe); //從容器中解析出中間件實(shí)例并且執(zhí)行handle方法 return call_user_func_array([$this->container->make($name), $this->method], //$passable就是請(qǐng)求實(shí)例,而$stack就是傳遞的閉包 array_merge([$passable, $stack], $parameters)); }
?? ?? ??:
? ??? ?? ???? ? ??? ?? ????? ?? ????? ?????. ??? ???? ??? ?? ???? ????? ???? 3? ?? ????? ???? 1? ?? ??? ???? ?????. array_reduce? ???? ??? ???? ?? ????? ????? ?? ?????.
??? ?? ?? array_reduce? ?? func3? ???? call_user_func(func3,$this->passable)? ???
return call_user_func($middleware[0]->handle, $this->passable, func2);
?? ????? ????? ?? ???? ???. ?? is:
public function handle($request, Closure $next) { return $next($request); }
??? return func2($request)? ?????. ??? $request? ?? ????? ?? ?????. ??? Zhengguo ????? ??? ???, ????? ? ?????. ?? ???? ?? ???? call_user_func? ???? ??? ????? ?? ??????
? ?? ?? ??? PHP ???? ?? ??? ??? ??? ????. Laravel ?????? ??? ?????.
Laravel ???? ?? ?? ? ?? ?? ?? ??? ??? PHP ??? ????? ??????!

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)