Created
November 25, 2017 03:08
-
-
Save abdumu/9ed567381cd79f60eaafde862dd63d06 to your computer and use it in GitHub Desktop.
How Laravel middleware system work
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//the middleware | |
$middleware1 = function ($request, $carry) { | |
echo "first middleware \n"; | |
return $carry($request); | |
}; | |
$middleware2 = function ($request, $carry) { | |
$response = $carry($request); | |
echo "\nsecond middleware \n"; | |
return $response; | |
}; | |
//the pipeline | |
$pipline = array_reduce( | |
[$middleware1, $middleware2], | |
function ($carry, $middleware) { | |
return function ($request) use ($carry, $middleware) { | |
return $middleware($request, $carry); | |
}; | |
}, | |
function ($request) { | |
echo $request->content(); | |
} | |
); | |
//run it | |
$pipline( | |
//think of it as a tiny request class | |
new class() { | |
public function content() | |
{ | |
return 'hello this is the content'; | |
} | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment