Cakephp execution process Code explanation

Source: Internet
Author: User
Recently, the PHP framework cakephp has been used for work. due to the fact that the PHP framework has been used less recently, and I have read some manuals recently, it is not very clear about some of the items in cakephp, so I decided to look at its source code. Below is some notes in the process of reading this document, it is best to open the corresponding php file for reference.

Recently, the PHP framework cakephp has been used for work. due to the fact that the PHP framework has been used less recently, and I have read some manuals recently, it is not very clear about some of the items in cakephp, so I decided to take a look at its source code. Below are some notes during my reading.

I think if you are interested in viewing this document, you 'd better open the corresponding php file and check it against it. Otherwise, you may not know what I'm talking about.

Start:

After downloading and installing from the Internet by default, there will be three directories, namely app, cake, and vendors, and the. htaccess, and index. php files.

Based on the instruction and experience in the manual, the cake directory is the core code of the framework and also a directory that we do not need to change during development. this will be helpful for us to upgrade the framework kernel in the future. If you need to write your own classes during the development process, you can put the files under the vendors directory and then use the app: import and other methods to call them .. Htaccess and index. php will pass the default access request to app/webroot/index. php file execution (such as not supported by the server. htaccess, you need to modify the index again. php file), and finally we can determine that the app is our default Main Battlefield! (Of course this can be changed. for example, when several applications share a kernel, I will not elaborate on it here)

When app/webroot/index is enabled. after the php file is written, we will find that the file is very simple. at first, a piece of code defines constants of some basic paths (such as app paths and more directory paths ), it contains a cake/bootstrap. php file, that is, the file loads all the information required by the framework and initializes the file. let's take a look at what it has done.

 

The following is some code in the bootstrap. php file. I added some comments to it.
If (! Isset ($ bootstrap )){
Require CORE_PATH. 'cake'. DS. 'basics. php'; // defines the common function methods of the framework.
$ TIME_START = getMicrotime ();
Require CORE_PATH. 'Cake '. DS. 'config'. DS. 'Paths. php'; // file path definition file
Require LIBS. 'object. php'; // the parent class of all classes
Require LIBS. 'inflector. php'; // The naming class for processing a singular or plural number. for example, inflector: pluralize ('example '), "examples" is returned"
Require LIBS. 'configure. php'; // configure the parameter class. an important loading class app: import is defined in the class. we can use it to import class files later.
}
Require LIBS. 'cache. php'; // load cache engine class

Configure: getInstance (); // initialize the configuration file. files such as app/config/core. php and app/config/bootstrap. php will be loaded here.

App: import ('core', array ('dispatcher '); // import path processing class, program entry path, etc.

The $ Dispatcher-> dispatch method execution program will be called at the end of the app/webroot/index. php file.
$ Dispatcher = new Dispatcher (); // initialize the path processing class
$ Dispatcher-> dispatch ($ url); // The framework starts to judge the URL execution program


Let's take a look at how the framework loads Dispatcher and executes the program!

 

First in cake/dispatcher. we will find the dispatch method in row 106 of the PHP file. by default, the parameter we pass to the dispatch will be blank. when the url parameter is blank, the framework automatically calls the getUrl method to obtain url and other information.
The code is as follows:

$ Url = $ this-> getUrl (); // get the URL path
$ This-> params = array_merge ($ this-> parseParams ($ url), $ additionalParams); // The array after the URL is processed, including $ _ POST, $ _ GET passed value and controller and method
The program uses the parseParams method to process URL parameters and call the routes class method to obtain information such as controller and action, assemble the information into a regular array, and pass it to $ this-> params. it is worth noting that, in the subsequent code, $ this-> params will be assigned to $ controller-> params, which is why we can use $ this-> params in the controller, for example: $ this-> params ['controller'] obtains the controller name of the current request.
Then, the current action will be judged. for example, if the action name contains an underscore (_), the protected method will not be accessed.
Some parameters will be assigned to the current controller.
The code is as follows:
$ Controller-> base = $ this-> base;
$ Controller-> here = $ this-> here;
$ Controller-> webroot = $ this-> webroot;
$ Controller-> plugin = $ this-> plugin;
$ Controller-> params = & $ this-> params; // pass all parameters to $ controller-> plugin, including the controller and action names, form data, and URL.
$ Controller-> action = & $ this-> params ['action'];
$ Controller-> passedArgs = array_merge ($ this-> params ['pass'], $ this-> params ['named']); // assign all parameters passed by $ _ GET to $ controller-> passedArgs, including whether there are named parameters, such as/controller/action/a: 1/B = 2/3/4

(Note: When the controller executes the render method, it will automatically pass some variables to the view, that is, the template we call. for example, the controller will assign the passedArgs variable value to passedArgs in the view, so that we can directly call $ this-> passedArgs in the template)
The framework determines whether $ _ POST and $ _ GET are equivalent in the current operation. for example, $ _ POST has fields named data, the framework Executes $ controller-> data = & $ this-> params ['data'];
The final modification method calls the current controller and passes the parameter execution


The code is as follows: return $ this-> _ invoke ($ controller, $ this-> params); // pass the parameter to $ controller through address reference, call $ controller, and formally execute

Next let's take a look at the called _ invoke
Function _ invoke (& $ controller, $ params)

Function _ invoke (& $ controller, $ params ){
$ Controller-> constructClasses (); // loads necessary information about the controller and merges appController (including loading model, helper, and Component). For more information, see controller. class methods in php
$ Controller-> Component-> initialize ($ controller); // call the initialize method of component before the controller beforeFilter
$ Controller-> beforeFilter ();
$ Controller-> Component-> startup ($ controller );

Here we can see that $ controller-> Component-> initialize is executed before $ controller-> beforeFilter (). I will not say much about this manual.
Note that
The $ controller-> constructClasses method merges some variables in the current custom controller class and AppController (app_controller.php), such as $ users, helper, and component, it is important to call the values under all $ users variables cyclically and load the corresponding model. if the $ this-> uses variable is false, no model is initialized: note: If you only want to define the controller and do not want to define the corresponding model file, this variable should be empty, or if you want to automatically load other models when the controller is called, you can assign the desired model name to $ this-> users = array ('modelname1', 'modelname2') through an array, in other cases, when the user does not set the value of $ users, the framework automatically calls the corresponding model based on the name (the model file is not required, but at this time, the database must have the corresponding table, or an error will be reported)


Other explanations are not required.

The following
$ Output = $ controller-> dispatchMethod ($ params ['action'], $ params ['pass']);
This method is used to call the dispatchMethod method in the object class, that is, the controller class executes the corresponding action method.

The following code is short:

If ($ controller-> autoRender ){
$ Controller-> output = $ controller-> render ();
} Elseif (empty ($ controller-> output )){
$ Controller-> output = $ output;
}
In $ controller, if we define $ this-> autoRender = false, the framework will not automatically call the render method.

If $ this-> autoRender is true or not defined, the framework calls the render function to call the corresponding template to display the final HTML output.


By now, the execution steps of this framework have basically ended. of course, there are still a lot of things that haven't been written in. one is because I have a limited level of writing, and the other is because there are too many owners who call the functions.

This is just a simplified execution process and does not involve model or other content. you can use it as a reference, because sometimes I feel that reading the source code is easier to understand than reading the manual.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.