YII2 请求源码分析

YII 一次路由请求
1、任何请求都会先到web/index.php 
看里面如何调转的。(new yii\web\Application($config))->run(); 可见调用了YII web下的 Application容器的run方法。
2.找到web下的Application容器。全局搜索run方法 缺找不到。发现他继承base下的Application。
3.找到base下的Application。见代码
abstract public function handleRequest($request);//抽象的处理请求方法,由子类重写了
public function run()
    {
        try {


            $this->state = self::STATE_BEFORE_REQUEST;
            $this->trigger(self::EVENT_BEFORE_REQUEST);


            $this->state = self::STATE_HANDLING_REQUEST;
            $response = $this->handleRequest($this->getRequest());//这个地方把请求交给了web下的Application


            $this->state = self::STATE_AFTER_REQUEST;
            $this->trigger(self::EVENT_AFTER_REQUEST);


            $this->state = self::STATE_SENDING_RESPONSE;
            $response->send();


            $this->state = self::STATE_END;


            return $response->exitStatus;


        } catch (ExitException $e) {


            $this->end($e->statusCode, isset($response) ? $response : null);
            return $e->statusCode;


        }
    }


======
4、查看 web下的Application的handleRequest方法
public $catchAll;//默认就是为空的
public function handleRequest($request)
    {
        if (empty($this->catchAll)) {
            list ($route, $params) = $request->resolve();//这里把请求处理结果赋值到$route、$params上
        } else {
            $route = $this->catchAll[0];
            $params = $this->catchAll;
            unset($params[0]);
        }
        try {
            Yii::trace("Route requested: '$route'", __METHOD__);
            $this->requestedRoute = $route;
            $result = $this->runAction($route, $params);
            if ($result instanceof Response) {
                return $result;
            } else {
                $response = $this->getResponse();
                if ($result !== null) {
                    $response->data = $result;
                }


                return $response;
            }
        } catch (InvalidRouteException $e) {
            throw new NotFoundHttpException(Yii::t('yii', 'Page not found.'), $e->getCode(), $e);
        }
    }













































  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值