thinkPHP life cycle

1, entry file

User-initiated request will file the application through the entrance, usually a  public/index.phpfile. Of course, you can also change or add a new entry file.

Typically codes are relatively simple entry file, a general file entry code is as follows:

// 应用入口文件

// 定义项目路径
define('APP_PATH', __DIR__ . '/../application/'); // 加载框架引导文件 require __DIR__ . '/../thinkphp/start.php'; 

In general entry file defines constants based support constants refer to the subsequent content or appendix.

In general, we do not recommend adding too much code in the application of import documents, especially related code and business logic.

2, boot files

Then there is the framework of the implementation of the boot file, start.phpthe file is a system default boot file. In the boot file, will perform the following operations in sequence:

  • Constant defines the loading system;
  • Load environment variables definition file;
  • Sign up automatic loading mechanism;
  • Registration error and exception handling mechanism;
  • Load convention profiles;
  • Execution of the application;

start.phpBoot files will first call the base.phpbase boot files, the following special requirements may introduce basic boot files directly in the file entry.

If you change the default boot file in your application gateways file, then follow the execution flow may change.

3, registration is automatically loaded

The system will call the  Loader::register()method to register automatically loaded after this is done, all compliant libraries (including Composerdependent loaded third-party libraries) will be loaded automatically.

The system is automatically loaded by a main part of the following components:

  1. Automatic loading method registration system \think\Loader::autoload
  2. Registration System namespace definitions
  3. Load library map file (if present)
  4. If there is Composerinstalled, log ** Composer** automatically load
  5. Registration extendExtended Directory

Automatically load a library for detection order:

  1. Whether defined library map;
  2. PSR-4Automatic loading detection;
  3. PSR-0Automatic loading detection;

You can see, define the mapping of the library is the most efficient.

4, registration error and exception mechanism

Execution Error::register()registration error and exception handling mechanism.

It consists of three parts:

  • Applications close method:think\Error::appShutdown
  • Error handling methods:think\Error::appError
  • Exception handling:think\Error::appException

Sign up for ease of use Close method is to intercept a number of system errors.

Throughout the life of the application request process, if an exception is thrown or serious error will result in early termination of the application, and an abnormal response and output an error message.

5, application initialization

The first step is to execute the application operation to initialize the application, including:

  • Load Application (Public) configuration;
  • Load Extension Configuration File (manufactured by extra_config_listdefinition);
  • Loading the application state configuration;
  • Load alias definitions;
  • Load Behavior is defined;
  • Load public (function) files;
  • Registration Application namespace;
  • Loading files spread function (by the extra_file_listdefinition);
  • Set the default time zone;
  • Loading system language pack;

6, URL access detection

After application initialization is complete, it will be accessed URL detection, including PATH_INFOdetection and URL suffix detection.

URL Access 5.0 must be a PATH_INFOURL address (including compatibility mode), for example:

http://serverName/index.php/index/index/hello/val/value 

So, if your environment can only support URL parameters common way to access, you must use

http://serverName/index.php?s=/index/index/hello&val=value 

If the file is a command-line access to the entrance below, then by

$php index.php index/index/hello/val/value... 

Get to normal $_SERVER['PATH_INFO']before proceeding parameters.

7, routing detection

If you turn the url_route_onargument, it will first be routed to detect the URL.

Once a match is detected if the route according to the routing address defined URL is registered to the corresponding scheduling.
5.0 supports the routing address as follows:

  • Routing to the module / controller / operator;
  • Routing address to redirect the outside;
  • The method of routing to the controller;
  • Routed to the closure function;
  • Routed to the methods of the class;

Routing address may be affected domain binding.

If you close detecting invalid default routing or identification analysis module / controller / operation is performed.

If you specify a scheduling application when the application is initialized, then the routing detection is optional.
May be used \ think \ App :: dispatch () for scheduling applications, for example:
the App :: dispatch ([ 'type' => 'Module1', 'Module1' => 'index / index']);

8, the distribution request

After completing the URL detection and routing testing, the router will distribute requests to the corresponding routing address, which is the life cycle of application requests the most important aspect.

In this step, to complete the application business logic and data is returned.

We recommend uniform use of returnthe returned data, rather than echooutput, if not necessary, do not use exitor dieinterrupt execution.

Direct echodata output will not be automatically converted output response convenience.

Here is the distribution request mechanism supported by the system, you can choose according to the situation:

Module / controller / operation

This is the default mechanism distribution request, the system determines the current request module, and the controller or routing operation name based on the URL address, and automatically call the appropriate access controller class, corresponding to the operation method is executed.
The following mechanisms, will first determine the current module, and module initialization operation (initializing operation of the application and the like), the module configuration parameters configuration parameters overrides the application not yet in force.

Support the mapping module, URL parameter is bound to a method, and an operation to bind some of the features and the like.

Controller method

And a manner similar to the former, but without determining module, controller and operator, a distribution request directly to a specific method of the controller class, so no initialization module.

External redirects

Distribution request directly to an external redirection address, supports the specified redirect code, default 301 redirect.

Closure function

When the routing address defined by the closure function can be directly used, complete some relatively simple logic operations and output.

Methods of the class

In addition to the above embodiment, the method also supports the distribution request class, comprising: a
static method:  'blog/:id'=>'\org\util\Blog::read'
class methods:'blog/:id'=>'\app\index\controller\Blog@read'

9, responsive to the output

The method of operation of the controller are all returnreturned instead of being output, the system calls the Response::sendmethod outputs the final application returns the data to the client or the page, and automatically converted into the default_return_typeformat of the configuration parameters. Therefore, the data output of the application executed only need to return to a normal PHP data.

10, the end of the application

In fact, after the application of the data in response to output, the application did not really end, the system will save the log write operation after the application or interrupt output.

The logging system includes an automatically generated log of user and system debug output, a write operation will be unified at the end of the time of application.

The impact of the write operation initialization log by log.

Guess you like

Origin www.cnblogs.com/tyblwmbs/p/10967480.html