摘要:好久沒有寫文章了,記錄一下這段時(shí)間學(xué)習(xí)的東西吧中間件是個(gè)非常方便的東西,能將一些邏輯實(shí)現(xiàn)解耦,并且在中,中間件的編寫也是非常的方便。對(duì)于的中間件,他的實(shí)現(xiàn)原理也是和這個(gè)一樣的。
好久沒有寫文章了,記錄一下這段時(shí)間學(xué)習(xí)的東西吧
laravel中間件是個(gè)非常方便的東西,能將一些邏輯實(shí)現(xiàn)解耦,并且在laravel中,
中間件的編寫也是非常的方便。誰(shuí)用誰(shuí)知道。
laravel中的中間件使用的就是裝飾器模式,什么是裝飾器模式,先去了解一下吧,這里大概說一下,就是這個(gè)模式主要的就是用于解決 當(dāng)一個(gè)類需要?jiǎng)討B(tài)擴(kuò)展功能的時(shí)候,使用繼承的方式會(huì)讓子類膨脹,并且這個(gè)擴(kuò)展的功能是個(gè)公用功能的情況下,不利于功能的復(fù)用以及代碼的解耦。
在laravel,使用對(duì)于使用這種模式的功能,稱為請(qǐng)求處理管道,也就是pipeline
//公共接口 interface middleware { public static function handle(Closure $next); } //裝飾器1 class MiddleStepOne implements middleware{ public static function handle(Closure $next) { echo "前期處理的第一步"."
"; $next(); echo "后期處理的第一步"."
"; } } //裝飾器2 class MiddleStepTwo implements middleware{ public static function handle(Closure $next) { echo "前期處理的第二步"."
"; $next(); echo "后期處理的第二步"."
"; } } function goFunc() { return function ($step,$className) { return function () use ($step,$className) { return $className::handle($step); }; }; } $pip = array( MiddleStepOne::class, MiddleStepTwo::class, ); $pip = array_reverse($pip); //反轉(zhuǎn)數(shù)組,以求達(dá)到要求的順序運(yùn)行 $first = function (){ echo "前期處理完畢"."
"; }; //實(shí)際要處理的函數(shù) $a = array_reduce($pip,goFunc(),$first); //遍歷pip數(shù)組,并將first作為第一個(gè)參數(shù)傳遞進(jìn)去 $a(); //執(zhí)行
輸出
這個(gè)就是一個(gè)簡(jiǎn)單的基于裝飾器模式的管道。他的本質(zhì)其實(shí)就是基于閉包和遞歸。
通過分析這個(gè)程序,對(duì)于最終生成的$a變量,它的值大概是這樣的 MiddleStepOne.handle(MiddleStepTwo.handle(first)),當(dāng)執(zhí)行的時(shí)候因?yàn)樵趆andle中有個(gè)next()函數(shù)的存在,所以這是一個(gè)遞歸的調(diào)用。對(duì)于laravel的中間件,他的實(shí)現(xiàn)原理也是和這個(gè)一樣的。
2.laravel中的中間件和請(qǐng)求處理管道在laravel中,我們我們可以通過設(shè)置中間件來在請(qǐng)求執(zhí)行之前做一些預(yù)先的處理。
從請(qǐng)求入口 public/index.php開始
重要的是這段代碼:即 處理請(qǐng)求,返回請(qǐng)求的響應(yīng)
$response = $kernel->handle(
$request = IlluminateHttpRequest::capture() //創(chuàng)建一個(gè)請(qǐng)求實(shí)例
);
接著我們進(jìn)入kernel中看他的具體實(shí)現(xiàn) IlluminateFoundationHttpKernel.php中
關(guān)于dispatchToRouter()函數(shù)請(qǐng)大家自己去看,這里就不多說了。
接下來就是激動(dòng)人心的PipeLine類了,
container = $container; } /** * Set the object being sent through the pipeline. * * @param mixed $passable * @return $this */ public function send($passable) { $this->passable = $passable; return $this; } /** * Set the array of pipes. * * @param array|mixed $pipes * @return $this */ public function through($pipes) { $this->pipes = is_array($pipes) ? $pipes : func_get_args(); return $this; } /** * Set the method to call on the pipes. * * @param string $method * @return $this */ public function via($method) { $this->method = $method; return $this; } /** * Run the pipeline with a final destination callback. * * @param Closure $destination * @return mixed */ public function then(Closure $destination) { $pipeline = array_reduce( array_reverse($this->pipes), $this->carry(), $this->prepareDestination($destination) ); return $pipeline($this->passable); } /** * Get the final piece of the Closure onion. * * @param Closure $destination * @return Closure */ protected function prepareDestination(Closure $destination) { return function ($passable) use ($destination) { return $destination($passable); }; } /** * Get a Closure that represents a slice of the application onion. * * @return Closure */ protected function carry() { return function ($stack, $pipe) { return function ($passable) use ($stack, $pipe) { if (is_callable($pipe)) { // If the pipe is an instance of a Closure, we will just call it directly but // otherwise we"ll resolve the pipes out of the container and call it with // the appropriate method and arguments, returning the results back out. //如果pip也就中間件函數(shù)是一個(gè)閉包可調(diào)用函數(shù),就直接返回這個(gè)閉包函數(shù)就行了 //這里我還沒有找到對(duì)應(yīng)的使用場(chǎng)景,后續(xù)補(bǔ)充 return $pipe($passable, $stack); } elseif (! is_object($pipe)) { list($name, $parameters) = $this->parsePipeString($pipe); // If the pipe is a string we will parse the string and resolve the class out // of the dependency injection container. We can then build a callable and // execute the pipe function giving in the parameters that are required. $pipe = $this->getContainer()->make($name); $parameters = array_merge([$passable, $stack], $parameters); } else { // If the pipe is already an object we"ll just make a callable and pass it to // the pipe as-is. There is no need to do any extra parsing and formatting // since the object we"re given was already a fully instantiated object. $parameters = [$passable, $stack]; } return method_exists($pipe, $this->method) ? $pipe->{$this->method}(...$parameters) : $pipe(...$parameters); }; }; } /** * Parse full pipe string to get name and parameters. * * @param string $pipe * @return array */ protected function parsePipeString($pipe) { list($name, $parameters) = array_pad(explode(":", $pipe, 2), 2, []); if (is_string($parameters)) { $parameters = explode(",", $parameters); } return [$name, $parameters]; } /** * Get the container instance. * * @return IlluminateContractsContainerContainer * @throws RuntimeException */ protected function getContainer() { if (! $this->container) { throw new RuntimeException("A container instance has not been passed to the Pipeline."); } return $this->container; } }
總的來說pipeLine類的實(shí)現(xiàn)和我之前寫的修飾器是差不多,這里主要麻煩的地方就在于就在于
protected function carry()函數(shù)內(nèi)部,對(duì)于當(dāng)pip是閉包,字符串,還有對(duì)象的處理。
之前覺得laravel的中間件是個(gè)很神秘的東西,但是看了之后才覺得也就那樣,很精巧,在實(shí)際開發(fā)中這種模式也是很有幫助的,例如我們目前用的一個(gè)gateway項(xiàng)目,因?yàn)闆]有使用任何框架,所以將判斷條件剝離,寫入到中間件中, 這樣實(shí)現(xiàn)了一定程度上的模塊化編程。
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/29482.html
摘要:而日志中間件則可以記錄所有傳入應(yīng)用程序的請(qǐng)求??蚣芤呀?jīng)內(nèi)置了一些中間件,包括維護(hù)身份驗(yàn)證保護(hù),等等。所有的中間件都放在目錄內(nèi)。在中可以使用授權(quán)策略來對(duì)用戶的操作權(quán)限進(jìn)行驗(yàn)證,在用戶未經(jīng)授權(quán)進(jìn)行操作時(shí)將返回異常。 這一節(jié)我們將給相關(guān)的動(dòng)作頁(yè)面添加權(quán)限,如已經(jīng)登錄的用戶將不會(huì)看到注冊(cè)、登錄按鈕,更不會(huì)對(duì)別人的個(gè)人資料進(jìn)行編輯操作,除非是管理員,這里我們將借助Laravel提供的中間件Mid...
摘要:官方地址是目前最流行的框架,發(fā)展勢(shì)頭迅猛,應(yīng)用非常廣泛,有豐富的擴(kuò)展包可以應(yīng)付你能想到的各種應(yīng)用場(chǎng)景,框架思想前衛(wèi),跟隨時(shí)代潮流,提倡優(yōu)雅代碼,自稱為工匠,其中的模板引擎容器以及擴(kuò)展包為業(yè)務(wù)的開發(fā)提供了極大的便利。 laravel5.5+ laravel官方地址 laravel是目前最流行的php框架,發(fā)展勢(shì)頭迅猛,應(yīng)用非常廣泛,有豐富的擴(kuò)展包可以應(yīng)付你能想到的各種應(yīng)用場(chǎng)景,lara...
摘要:接著上篇分割線是的實(shí)例,但是文件中找不到方法在類內(nèi)部看到,打開找到了方法,方法注釋寫的是主要用于運(yùn)行應(yīng)用以及發(fā)送響應(yīng)主要看方法 接著上篇$app->run();--------------------分割線------------------------ $app是Application的實(shí)例,但是Application.php文件中找不到run方法在類內(nèi)部看到use Concerns...
摘要:學(xué)習(xí)筆記之已經(jīng)聊過使用了來設(shè)計(jì),看源碼發(fā)現(xiàn)其巧妙用了和的一些數(shù)組函數(shù)來設(shè)計(jì)。開發(fā)環(huán)境內(nèi)置函數(shù)和看源碼之前,先看下這幾個(gè)內(nèi)置函數(shù)的使用。學(xué)習(xí)筆記之實(shí)例化源碼解析已經(jīng)聊過的實(shí)例化,得到中的變量,即的實(shí)例化對(duì)象。后面再學(xué)習(xí)下的源碼,到時(shí)見。 說明:本文主要學(xué)習(xí)Laravel的Middleware的源碼設(shè)計(jì)思想,并將學(xué)習(xí)心得分享出來,希望對(duì)別人有所幫助。Laravel學(xué)習(xí)筆記之Decorato...
摘要:本文主要學(xué)習(xí)總結(jié)下間參數(shù)傳遞。開發(fā)時(shí)經(jīng)常碰到類似場(chǎng)景有時(shí)需要在中讀取中設(shè)置的和,有時(shí)也需要在中讀取中設(shè)置的參數(shù)??偨Y(jié)下這幾個(gè)知識(shí)點(diǎn),便于查閱。 本文主要學(xué)習(xí)總結(jié)下Route,Middleware,Controller間參數(shù)傳遞。開發(fā)時(shí)經(jīng)常碰到類似場(chǎng)景:有時(shí)需要在Middleware中讀取Route中設(shè)置的middleware parameter和route parameter,有時(shí)也需...
閱讀 1616·2023-04-25 15:50
閱讀 1319·2021-09-22 15:49
閱讀 2950·2021-09-22 15:06
閱讀 3620·2019-08-30 15:54
閱讀 2346·2019-08-29 11:33
閱讀 2133·2019-08-23 17:56
閱讀 2168·2019-08-23 17:06
閱讀 1309·2019-08-23 15:55