Skip to content

Instantly share code, notes, and snippets.

@abdumu
Created November 25, 2017 03:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abdumu/9ed567381cd79f60eaafde862dd63d06 to your computer and use it in GitHub Desktop.
Save abdumu/9ed567381cd79f60eaafde862dd63d06 to your computer and use it in GitHub Desktop.
How Laravel middleware system work
<?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