最新消息:欢迎加入小松的QQ群一起讨论一起学习,搜索页面修改成lua+ElasticSearch,使用php这个页面速度相对慢一点,优化之后速度杠杠的,如有问题请加群联系我

laravel 请求处理通道

PHP 81浏览 0评论

laravel请求处理通道之后其实是一个装饰者模式做的,目的是为了解耦
一个简单的装饰者模式的例子

interface Decorator{
    public function display();
}
class XiaoFang implements Decorator{
    private $name;
    public function __construct($name){
        $this->name=$name;
    }
    public function display(){
        echo "我是".$this->name.",我出门了";
    }
}
class Finery implements Decorator{
    private $component;
    public function __construct(Decorator $component){
        $this->component=$component;
    }
    public function display(){
        echo $this->component->display();
    }
}
class Shoes extends Finery{
    public function display(){
        echo  "穿上鞋子";
        parent::display();
    }
}
class Fire extends Finery{
    public function display(){
        echo  "出门前整理头发<br/>";
        parent::display();
        echo  "出门后整理一下头发<br/>";
    }
}
$xiaofang=new XiaoFang("小芳");
$shoes=new Shoes($xiaofang);
$shkirt=new Shoes($shoes);
$fire=new Fire($skirt);
$fire->display();

laravel 一个通道简单实例

interface Step{
    public static functin go(Closure $next);
}
class FirstStep implements Step{
    public static function go(Closure $next){
        echo "开启session,获取数据<br/>";
        $next();
        echo "保持数据,关闭session<br/>";
    }
}
function goFun($step,$className){
    return function()use($step,$className){
        return $className::go($step);
    }
}
function then(){
    $steps=["FirstSep"];
    $prepare=function(){"echo 请求路由传递,返回响应<br/>"};
    $go=array_reduce($steps,"goFun",$prepare);
    $go();
}
then();

上面的例子是《laravel框架关键技术解析》这本书上的

laravel 请求处理管道
文件 Illuminate/Foundation/Http/Kernel.php

class Kernel implements KernelContract
{
    //....
    protected function sendRequestThroughRouter($request)
    {
        $this->app->instance('request', $request);

        Facade::clearResolvedInstance('request');

        $this->bootstrap();

        return (new Pipeline($this->app))
                    ->send($request)
                    ->through($this->app->shouldSkipMiddleware() ? [] : $this->middleware)
                    ->then($this->dispatchToRouter());
    }
    //...
}

文件Illuminate/Pipeline/Pipeline.php

    public function then(Closure $destination)
    {
        $firstSlice = $this->getInitialSlice($destination);

        $pipes = array_reverse($this->pipes);

        return call_user_func(
            array_reduce($pipes, $this->getSlice(), $firstSlice), $this->passable
        );
    }

larave中这块内容还是比较重要的

QQ交流群:136351212(满) 455721967

如无特别说明,本站文章皆为原创,若要转载,务必请注明以下原文信息:
转载保留版权:小松博客» laravel 请求处理通道
本文链接地址:https://www.phpsong.com/3035.html

发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
木有头像就木有JJ!点这里按步骤申请Gravatar头像吧!