yii2 底层加载

生命周期
在index文件中包含了composer自动加载和yii.php注册函数;包含了config配置文件
index.php 入口文件 ->run方法
Application类里面调用handleRequest方法中的$require->resolve方法使用的url组件解析路由,解析成controller和action两个变量 base\moduler\runAction方法 创建控制器实例 创建动作 执行过滤 执行动作加载数据渲染视图响应处理组件

入口文件里面执行yii\web\Application::run()时,开始进入路由阶段

1.yii\web\Application里的handleRequest方法中的$request->resolve()就是用来解析路由。

复制代码
public function handleRequest(KaTeX parse error: Expected '}', got 'EOF' at end of input: … if (empty(this->catchAll)) {//catchAll用于拦截所有请求,默认不拦截
list ($route, $params) = r e q u e s t − > r e s o l v e ( ) ; / / 经 过 后 面 的 分 析 , 此 处 request->resolve();//经过后面的分析,此处 request>resolve();//route = G E T [ ′ r ′ ] , _GET['r'], GET[r],params = $_GET;
} else {//拦截后替代的请求配置在catchAll里面
$route = $this->catchAll[0];
$params = t h i s − > c a t c h A l l ; u n s e t ( this->catchAll; unset( this>catchAll;unset(params[0]);
}
try {
Yii::trace(“Route requested: ‘$route’”, METHOD);
$this->requestedRoute = $route;
$result = t h i s − > r u n A c t i o n ( this->runAction( this>runAction(route, p a r a m s ) ; / / 执 行 请 求 i f ( params);//执行请求 if ( params);//if(result instanceof Response) {
return $result;
} else {
$response = t h i s − > g e t R e s p o n s e ( ) ; i f ( this->getResponse(); if ( 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.接下来我们具体分析一下yii\web\Request::resolve方法

复制代码
public function resolve()
{
r e s u l t = Y i i : : result = Yii:: result=Yii::app->getUrlManager()->parseRequest(KaTeX parse error: Expected 'EOF', got '\web' at position 17: …his);//实际调用了yii\̲w̲e̲b̲\UrlManage的pars…result = [ G E T [ ′ r ′ ] , [ ] ] i f ( _GET['r'],[]] if ( GET[r],[]]if(result !== false) {
list ($route, $params) = r e s u l t ; / / result;// result;//route = G E T [ ′ r ′ ] , _GET['r'], GET[r],params = []
if ($this->_queryParams === null) {
$_GET = $params + $_GET; // preserve numeric keys
} else {
$this->_queryParams = $params + KaTeX parse error: Expected 'EOF', got '}' at position 33: …s; }̲ re…route, t h i s − > g e t Q u e r y P a r a m s ( ) ] ; / / r e t u r n [ this->getQueryParams()];//return [ this>getQueryParams()];//return[_GET[‘r’],$_GET]
} else {
throw new NotFoundHttpException(Yii::t(‘yii’, ‘Page not found.’));
}
}
复制代码

3.yii\web\UrlManage::parseRequest方法

复制代码
public function parseRequest(KaTeX parse error: Expected '}', got 'EOF' at end of input: … { if (this->enablePrettyUrl) {//默认enablePrettyUrl为false,这里面是pretty url方式的解析工作,不做分析
$pathInfo = $request->getPathInfo();
/* @var r u l e U r l R u l e ∗ / f o r e a c h ( rule UrlRule */ foreach ( ruleUrlRule/foreach(this->rules as KaTeX parse error: Expected '}', got 'EOF' at end of input: … if ((result = r u l e − > p a r s e R e q u e s t ( rule->parseRequest( rule>parseRequest(this, $request)) !== false) {
return $result;
}
}

        if ($this->enableStrictParsing) {
            return false;
        }

        Yii::trace('No matching URL rules. Using default URL parsing logic.', __METHOD__);

        $suffix = (string) $this->suffix;
        if ($suffix !== '' && $pathInfo !== '') {
            $n = strlen($this->suffix);
            if (substr_compare($pathInfo, $this->suffix, -$n, $n) === 0) {
                $pathInfo = substr($pathInfo, 0, -$n);
                if ($pathInfo === '') {
                    // suffix alone is not allowed
                    return false;
                }
            } else {
                // suffix doesn't match
                return false;
            }
        }

        return [$pathInfo, []];
    } else {
        Yii::trace('Pretty URL not enabled. Using default URL parsing logic.', __METHOD__);
        $route = $request->getQueryParam($this->routeParam, '');//$this->routeParam默认为r,这里实际上是$route = $_GET['r'];
        if (is_array($route)) {
            $route = '';
        }

        return [(string) $route, []];//所以此方法默认返回为[$_GET['r'],[]]
    }
}

复制代码

4.执行请求,yii\base\Module::runAction

复制代码
public function runAction($route, $params = [])
{
$parts = t h i s − > c r e a t e C o n t r o l l e r ( this->createController( this>createController(route);//将 G E T [ ′ r ′ ] 解 析 为 实 际 的 c o n t r o l l e r 和 a c t i o n i f ( i s a r r a y ( _GET['r']解析为实际的controller和action if (is_array( GET[r]controlleractionif(isarray(parts)) {
/* @var c o n t r o l l e r C o n t r o l l e r ∗ / l i s t ( controller Controller */ list( controllerController/list(controller, $actionID) = $parts;
o l d C o n t r o l l e r = Y i i : : oldController = Yii:: oldController=Yii::app->controller;
Yii::$app->controller = $controller;
$result = c o n t r o l l e r − > r u n A c t i o n ( controller->runAction( controller>runAction(actionID, p a r a m s ) ; / / 执 行 c o n t r o l l e r 里 面 的 a c t i o n 方 法 Y i i : : params);//执行controller里面的action方法 Yii:: params);//controlleractionYii::app->controller = $oldController;

        return $result;
    } else {
        $id = $this->getUniqueId();
        throw new InvalidRouteException('Unable to resolve the request "' . ($id === '' ? $route : $id . '/' . $route) . '".');
    }
}

复制代码

综合以上方法,yii默认将请求参数里面的r(即$_GET[‘r’])解析成相应的controller和action,并执行@TOC

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值