Last active
November 7, 2018 12:24
-
-
Save diloabininyeri/eef9076f7f8855cc62d17cfa790c25c0 to your computer and use it in GitHub Desktop.
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 | |
/** | |
* @author dılo sürücü <[email protected]> | |
* php pure from structure basic middleware example | |
* | |
*/ | |
Class MiddlewareFirst | |
{ | |
/** | |
* @param Closure $next | |
* @param stdClass $request | |
* @return mixed | |
* | |
*/ | |
function handle(Closure $next, stdClass $request) | |
{ | |
return $next($request); | |
} | |
} | |
class MiddlewareSecond | |
{ | |
/** | |
* @param $next | |
* @param $request | |
* @return mixed | |
* | |
*/ | |
function handle(Closure $next, stdClass $request) | |
{ | |
return $next($request); | |
} | |
} | |
class MyController | |
{ | |
function getRequestId(stdClass $request) | |
{ | |
echo $request->id; | |
} | |
} | |
$middleWares = ["MiddlewareFirst", "MiddlewareSecond"]; | |
$request = new stdClass(); | |
$request->id = 23; | |
$countMiddleWare = count($middleWares); | |
for ($a = 0; $a < $countMiddleWare; $a++) { | |
call_user_func_array([new $middleWares[$a], "handle"], [function ($request) use ($middleWares, $a, $countMiddleWare) { | |
if ($middleWares[$a] == $middleWares[$countMiddleWare - 1]) { | |
return (new MyController())->getRequestId($request); | |
} | |
}, $request]); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment