Detailed explanation of Yii2.0 execution process
This article mainly shares with you the detailed explanation of Yii2.0 execution process, mainly in the form of code, hoping to help everyone.
index.php 2 ---->引入 vendor/auto_load.php 3 auto_load.php 4 ---->引入 ventor/composer/autoload_real.php 5 ---->执行 ComposerAutoloaderInit240f916b39e20bc11bc03e2039805bd4->getLoader 6 autoload_real.php 7 ---->getLoader 8 ---->单例 9 ---->spl_autoload_register(array('ComposerAutoloaderInit240f916b39e20bc11bc03e2039805bd4','loadClassLoader')) 10 ---->self::$loader = new \Composer\Autoload\ClassLoader(); 11 ---->引入 \Composer\Autoload\ClassLoader 12 ---->引入 autoload_namespaces.php 给作为属性 $loader 13 ---->$vendorDir $baseDir 14 ---->引入 autoload_psr4.php 作为属性给 $loader 15 ---->$loader->register(true); 16 ---->spl_autoload_register this,loadClass 17 ---->loadClass ----> findFile 18 ---->引入 autoload_files.php require 19 ---->return $loader 20 index.php 21 ---->初始化了一个$loader (暂时不知道什么用) 22 ---->引入 /vendor/yiisoft/yii2/Yii.php 23 Yii.php 24 ----> 引入 BaseYii.php ,Yii 继承 BaseYii 25 ---->spl_autoload_register(BaseYii,autoload) 26 ---->Yii::$classMap = include(__DIR__ . '/classes.php'); //引入一堆php class地址 27 ---->Yii::$container = new yii\di\Container;//容器 28 29 //继承关系梳理 30 yii\di\Container(容器) -> yii\base\Component(实现 属性,事件,行为 功能的基类) -> Object(实现 属性 功能的基类,含有__construct) 31 yii\web\Application(所有web应用类的基类) -> \yii\base\Application(所有应用的基类,__construct) -> Module(所有模块和应用的基类,含有__construct) -> yii\di\ServiceLocator(服务定位器,包含所有模块和应用) -> yii\base\Component -> Object 32 33 index.php 34 ---->$config 引入 35 (new yii\web\Application($config))->run(); 36 ---->\yii\base\Application __construct() 37 ---->Yii::$app = $this (Application) 38 ---->$this->setInstance($this); 设置当前请求类的实例 (把Application类的对象push进Yii的loadedModules里) 39 ---->$this->preInit($config); 40 ---->$this->_basePath = $_config['basepath'] Yii::aliases[@app] = $_config['basepath'] 41 ---->$this->getVendorPath 设置框架路径 42 ---->setVendorPath Yii::aliases[@vendor] Yii::aliases[@bower] Yii::aliases[@npm] 43 ---->$this->getRuntimePath 同上,设置runtimePath Yii::aliases[@runtime] 44 ---->setTimeZone 设置时区 45 ---->核心组件信息(地址)注入$config log view formatter i18n mailer urlManager assetManager security 46 ---->registerErrorHandler 定义错误处理程序 47 ---->Component::__construct($config); Object中的__construct //这步发生了很多事情 48 ---->Yii::configure($this) 把$config赋给$this作属性 49 50 ? $this->bootstrap 中的值哪来的 ?---->配置文件来的。。。。 51 52 ---->$this->init() 53 ---->$this->bootstrap(); 初始化扩展和执行引导组件。 54 ---->引入@vendor/yiisoft/extensions.php 55 ---->Yii::aliases['xxx'] = 'xxx'; extensions.php中aliase地址 56 <!-- 初始化完成 --> 57 58 ---->\yii\base\Application->run() 59 ---->$this->trigger($name) --- $event = new Event; //$name = beforeRequest 执行 _event[beforeRequest]handler 60 ---->$event->sender = application object 61 $event->name = $name; 62 //这两句没懂 63 $event->data = $handler[1]; 64 call_user_func($handler[0], $event); 65 Event::trigger($this, $name, $event); //$this = application object 66 67 ---->$response = $this->handleRequest($this->getRequest()); 68 ---->$this->getRequest() ---->get('request') get方法位于ServiceLocator ,返回指定id的实例(返回request实例到_components['request']) 69 ---->$this->handleRequest(request对象) //request对象的类是yii/web/request 70 ---->list ($route, $params) = $request->resolve();//解决当前请求的路由和相关参数 71 ---->$params 放置地址栏解析的结果数组Array ( [0] => [1] => Array ( [m] => sds [c] => dasd ) ) 72 ---->runAction($route, $params); //位于Module 73 ---->list($controller, $actionID) = $this->createController($route) 返回array('0'=>控制器controller对象,'1'=>'action名') 74 $controller 赋给Yii::$app->controller 75 ---->$controller->runAction($actionID, $params); yii/base/Controller 76 77 ---->runAction($actionID, $params); yii/base/Controller 78 ---->$action = $this->createAction($id); //生成一个InlineAction对象,赋给Yii::$app->requestedAction 79 InlineAction __construct $this->actionMethod = $actionMethod; 80 ---->beforeAction 81 ---->$action->runWithParams($params); //位于 yii/base/InlineAction 82 ---->$args = $this->controller->bindActionParams($this, $params);//位于yii/web/controller $this=>InlineAction $params=>模块/控制器 数组 --- 将参数绑定到action,返回有效参数数组$args 83 ---->赋给Yii::$app->requestedParams = $args; 84 ---->call_user_func_array([$this->controller, $this->actionMethod], $args) //执行第一个回调函数 真正执行 85 ---->afterAction 86 ---->返回执行结果(页面已出) 给Module中的runAction 87 88 ---->返回结果给handleRequest 89 ---->$response = $this->getResponse(); 返回一个response对象,具体同上 90 ---->$response->data = $result; 91 ---->返回$response给yii/base/Application 的 run $response 92 ---->$response->send();输出内容 93 <!-- 页面输出完成 --> 94 95 96
The above is the detailed content of Detailed explanation of Yii2.0 execution process. For more information, please follow other related articles on the PHP Chinese website!

The main advantages of using database storage sessions include persistence, scalability, and security. 1. Persistence: Even if the server restarts, the session data can remain unchanged. 2. Scalability: Applicable to distributed systems, ensuring that session data is synchronized between multiple servers. 3. Security: The database provides encrypted storage to protect sensitive information.

Implementing custom session processing in PHP can be done by implementing the SessionHandlerInterface interface. The specific steps include: 1) Creating a class that implements SessionHandlerInterface, such as CustomSessionHandler; 2) Rewriting methods in the interface (such as open, close, read, write, destroy, gc) to define the life cycle and storage method of session data; 3) Register a custom session processor in a PHP script and start the session. This allows data to be stored in media such as MySQL and Redis to improve performance, security and scalability.

SessionID is a mechanism used in web applications to track user session status. 1. It is a randomly generated string used to maintain user's identity information during multiple interactions between the user and the server. 2. The server generates and sends it to the client through cookies or URL parameters to help identify and associate these requests in multiple requests of the user. 3. Generation usually uses random algorithms to ensure uniqueness and unpredictability. 4. In actual development, in-memory databases such as Redis can be used to store session data to improve performance and security.

Managing sessions in stateless environments such as APIs can be achieved by using JWT or cookies. 1. JWT is suitable for statelessness and scalability, but it is large in size when it comes to big data. 2.Cookies are more traditional and easy to implement, but they need to be configured with caution to ensure security.

To protect the application from session-related XSS attacks, the following measures are required: 1. Set the HttpOnly and Secure flags to protect the session cookies. 2. Export codes for all user inputs. 3. Implement content security policy (CSP) to limit script sources. Through these policies, session-related XSS attacks can be effectively protected and user data can be ensured.

Methods to optimize PHP session performance include: 1. Delay session start, 2. Use database to store sessions, 3. Compress session data, 4. Manage session life cycle, and 5. Implement session sharing. These strategies can significantly improve the efficiency of applications in high concurrency environments.

Thesession.gc_maxlifetimesettinginPHPdeterminesthelifespanofsessiondata,setinseconds.1)It'sconfiguredinphp.iniorviaini_set().2)Abalanceisneededtoavoidperformanceissuesandunexpectedlogouts.3)PHP'sgarbagecollectionisprobabilistic,influencedbygc_probabi

In PHP, you can use the session_name() function to configure the session name. The specific steps are as follows: 1. Use the session_name() function to set the session name, such as session_name("my_session"). 2. After setting the session name, call session_start() to start the session. Configuring session names can avoid session data conflicts between multiple applications and enhance security, but pay attention to the uniqueness, security, length and setting timing of session names.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version
God-level code editing software (SublimeText3)