接下来简单举个例子,使用装饰者模式实现维护session实现。
一、没有使用装饰者模式,需要对模块(welcomecontroller::index方法)进行修改。
class welcomecontroller{ public function index() { echo 'session start.', php_eol; echo 'hello!', php_eol; echo 'session close.', php_eol; }}
二、使用装饰者模式,$pipelist表示需要执行的中间件数组。关键在于使用了array_reduce函数。
class welcomecontroller{ public function index() { echo 'hello!', php_eol; }}interface middleware{ public function handle(closure $next);}class seesion implements middleware{ public function handle(closure $next) { echo 'session start.', php_eol; $next(); echo 'session close.', php_eol; }}$pipelist = [ "seesion",]; function _go($step, $classname){ return function () use ($step, $classname) { $o = new $classname(); return $o->handle($step); };} $go = array_reduce($pipelist, '_go', function () { return call_user_func([new welcomecontroller(), 'index']);});$go();
更多php相关知识,请访问!
以上就是php laravel请求处理管道(装饰者模式)的详细内容。
