Yii2框架解剖

it2022-05-25  78

Application => web\Application => Module => ServiceLocator => Component index.php - require vendor/autoload.php - require Yii.php extends \yii\BaseYii - Yii::$classMap - Yii::$container = new yii\di\Container(); - BaseYii 定义全局配置常量 - require web/config.php - (new yii\web\Application($config))->run(); ** 构造方法流程: — Yii::$app = $this — preInit工作: 1)设定$config的各种初始值: $config[‘basePath’],$config[‘vendorPath’],$config[‘runtimePath’],$config[‘timeZone’], $config[‘components’] 2)$config[‘components’]主要包含: a)配置文件 'request','cache', ‘user',’errorHandler',’mailer',’log','db’ ,’urlManager' b)base\Application::coreComponents() log,view,formatter,i18n,mailer,urlManager,assetManager,security c)Application::coreComponents() request, response, session, user, errorHandler 3)DEV环境: $config[‘bootstrap’] => [‘debug’, ‘gii'] $config[‘modules’] => [‘debug’, ‘gii'] — 注册错误处理模块 registerErrorHandler $this->set('errorHandler', $config['components']['errorHandler']); — Component::__construct($config); 1)用$config初始化当前应用对象 Yii::configure($this, $config); // 将config配置中的key=>value导出到$this a)属性存在,$this->$name = $value b)属性不存在,走Component::__set() components => $this->_definitions modules => $this->_modules traceLevel、levels 2)执行初始化方法,base\Application::init() a)$this->state = self::STATE_INIT; b)Application::bootstrap() i)获取请求对象,$request = $this->getRequest(); ii)设置别名,@webroot、@web iii)base\Application::bootstrap() 1)$this->extensions = include('yiisoft/extensions.php') yii2-swiftmailer、yii2-codeception、yii2-bootstrap、yii2-debug、yii2-gii、yii2-faker 2)如果extensions有别名配置,为extensions设置别名(都有) 3)如果extensions有bootstrap配置,则执行对应的bootstrap()(暂无) 4)如果$this->bootstrap()中的模块实现了BootstrapInterface,则执行对应的bootstrap() - debug - 注册事件->EVENT_BEFORE_REQUEST->EVENT_END_BODY->renderToolbar() - 添加路由规则,/debug ** run方法流程 1)触发事件,EVENT_BEFORE_REQUEST getView()::on(View::EVENT_END_BODY, [$this, 'renderToolbar']) 2)处理请求,Application::handleRequest() a)Request::resolve(); i)获取 $route、$params b)动作执行,Module::runAction($route, $params); i)创建控制器,Module::createController($route),生成controller、actionId ii)控制器动作执行,Controller::runAction($actionId, $params) 1)创建动作,createAction($actionId) 2)Component::beforeAction过滤 $event = new ActionEvent($action); Component::trigger(self::EVENT_BEFORE_ACTION, $event); a)Component::ensureBehaviors() i)获取controller::behaviors()并绑定行为,Component::attachBehaviorInternal 1)ActionFilter::attach($this); $owner->on(Controller::EVENT_BEFORE_ACTION, [$this, 'beforeFilter']); 2)$this->_behaviors[$name] = $behavior; 3)Controller::beforeAction() a)$this->trigger(self::EVENT_BEFORE_ACTION, $event); i)ActionFilter::beforeFilter() 1)AcessControl::beforAction() a)AccessRule::allows() b)call_user_func($rule->denyCallback, $rule, $action); c)call_user_func($this->denyCallback, $rule, $action); d)$this->denyAccess($user); yii\web\User::loginRequired() => Yii::$app->getResponse()->redirect($this->loginUrl); 2)注册on(Controller::EVENT_AFTER_ACTION, [$this, 'afterFilter'], null, false); iii)InlineAction::runWithParams() a)调用Controller内的action方法,call_user_func_array([$controller, $actionMethod], $args); b)action方法内执行render等渲染方法 iv)Controller::afterAction $this->trigger(self::EVENT_AFTER_ACTION, $event); c)response->data = $result - response->data = $result 3)触发事件EVENT_AFTER_REQUEST - $response->send(); 4)响应发送到客户端,$response->send(); a)触发事件,EVENT_BEFORE_SEND b)输出准备工作,Response::prepare() c)触发事件,EVENT_AFTER_PREPARE d)发送响应头,Response::sendHeaders() e)发送响应内容,Response::sendContent() f)触发事件,EVENT_AFTER_SEND

转载于:https://www.cnblogs.com/chenguoli/p/7607245.html

相关资源:各显卡算力对照表!

最新回复(0)