Home Backend Development PHP Tutorial cakephp execution process code interpretation

cakephp execution process code interpretation

Dec 20, 2016 am 09:40 AM

Recently, I need to use the cakephp PHP framework for my work. Since I have used it less before, and I recently read some manuals, I feel that some things in cakephp are not very clear, so I decided to take a look at its source code. Here is what I saw Some notes during the process

I think if you are interested in viewing this document, it is best to open the corresponding PHP file and compare it, otherwise you may not know what I am talking about.

Start:

When we download and install by default from the Internet, there will be three directories: app, cake, vendors, as well as .htaccess, and index.php files.

According to the manual instructions and experience, the cake directory is the core code of the framework and is also a directory that does not need to be touched during development. This will be helpful for us to upgrade the framework core in the future. If you need to write your own class during the development process, you can place the file in the vendors directory and call it using methods such as app::import. .htaccess and index.php will pass the default access request to the app/webroot/index.php file for execution (if the server does not support .htaccess, you need to modify the index.php file again). Finally, we can determine that app is our default Main battlefield! (Of course this can be changed, for example, if several applications share a core, I won’t go into details here)

When opening the app/webroot/index.php file, we will find that this file writes It is very simple. The first piece of code defines some basic path constants (such as app path, directory path, etc.), followed by a cake/bootstrap.php file, which is this file that loads all the necessary information and initialization of the framework. Work, let's see what it does. B 是 Below is part of the code in the bootstrap.php file. I appropriately add some comments

If (! Isset ($ BOOTSTRAP)) {

Require core_path. 'Cake'. Ds. 'Basics.php' ; //Defines the common function methods of the framework

                                                                           . 'Object.php'; // The parent class of all classes

Require Libs. 'Inflector.php'; // Naming category, handling single plural hump naming: For example
                                                                                               ,,,,                                      ,,,                  ,,,,            ,,,              require LIBS . '; //Load cache engine class

                                                       use using ‐ ‐ ‐ ‐ ‐‐‐‐‐‐‐‐'; ​          'Core', array('Dispatcher')); //Import path processing class, program entry path, etc.

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

Let's see how the framework loads Dispatcher and executes the program!

First we will find the dispatch method in line 106 of the cake/ dispatcher.php file. By default, the parameters we pass to dispatch will be empty. When the url parameter is empty, the framework will automatically call the getUrl method to get URL and other information

Code such as:

$url = $this->getUrl(); //Get the URL path
$this->params = array_merge($this->parseParams($url), $additionalParams); //Get the processed URL Arrays, including values ​​passed by $_POST and $_GET, as well as controllers and methods. The program processes URL parameters through the parseParams method and calls the routes class method to obtain controller and action information, assemble it into a regular array, and pass it to $this- >params, it is worth noting that in the following 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'] will get the controller name of the current request
Then the current action will be judged. For example, if there is an underscore (_) before the action name, it will be considered a protected method and will not be used. Access, etc.
Next, 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 controller and action names, form data, URL, etc.
$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, which is what we call the template. For example, the controller will assign the value of the passedArgs variable. Give passedArgs in the view, so that we can directly call $this->passedArgs in the template)

It should be noted here that the framework will determine whether there are $_POST, $_GET and other values ​​​​in the current operation. For example, $_POST has a field name For the data field, the framework will execute $controller->data =& $this->params['data'];

The last modified method will call the current controller and pass the parameters for execution

The code is as follows: return $this- >_invoke($controller, $this->params); //Pass parameters to $controller through address reference, call $controller, and officially execute


Let’s take a look at the called _invoke

function _invoke(&$controller, $params)


function _invoke(&$controller, $params) {

$controller->constructClasses(); //Load controller necessary information and merge appController, etc. (including loading model, helper and Component), more information Please check the class method under controller.php

$controller->Component->initialize($controller); //Call component's initialize method before controller beforeFilter
$controller->beforeFilter();
$controller ->Component->startup($controller);

Here we can see that $controller->Component->initialize is executed before $controller->beforeFilter(), as shown in this manual I won’t say much more when it comes to mentioning it. What should be noted here is that the $controller->constructClasses method will merge the current user-defined controller class and some variables in AppController (app_controller.php), such as $users, helper and component, etc. , the more important thing here is that it will cycle through all the values ​​under the $users variable and load the corresponding model. If the $this->uses variable is false, no model will be initialized: Note that 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'), etc. Another situation is that when the user does not set the value of $users himself, the framework will automatically try to call the corresponding model based on the name (the model file is not necessary, but at this time in the database There must be a corresponding table, otherwise an error will be reported)


Other explanations should not be required

The following

$output = $controller->dispatchMethod($params['action'], $params['pass ']);
This method is to call the dispatchMethod method in the object class. In fact, the controller class executes the corresponding action method

The following is a small piece of code

if ($controller->autoRender) {
                                                                                                                                         output = $ output; When the time comes, the framework will call the render function to call the corresponding template to display and output the final HTML


The execution steps of the framework are basically over. Of course, there are still many things that have not been written in. Firstly, my writing skills are limited, and secondly, calling There are too many functions, so I won’t explain them one by one here.

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

The above is the content of cakephp execution process code interpretation. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

How to use the database query builder in CakePHP? How to use the database query builder in CakePHP? Jun 04, 2023 am 09:02 AM

CakePHP is an open source PHPMVC framework which is widely used in web application development. CakePHP has many features and tools, including a powerful database query builder for interactive performance databases. This query builder allows you to execute SQL queries using object-oriented syntax without having to write cumbersome SQL statements. This article will introduce how to use the database query builder in CakePHP. Establishing a database connection Before using the database query builder, you first need to create a database connection in Ca

How to create custom pagination in CakePHP? How to create custom pagination in CakePHP? Jun 04, 2023 am 08:32 AM

CakePHP is a powerful PHP framework that provides developers with many useful tools and features. One of them is pagination, which helps us divide large amounts of data into several pages, making browsing and manipulation easier. By default, CakePHP provides some basic pagination methods, but sometimes you may need to create some custom pagination methods. This article will show you how to create custom pagination in CakePHP. Step 1: Create a custom pagination class First, we need to create a custom pagination class. this

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

How does CakePHP handle file uploads? How does CakePHP handle file uploads? Jun 04, 2023 pm 07:21 PM

CakePHP is an open source web application framework built on the PHP language that simplifies the development process of web applications. In CakePHP, processing file uploads is a common requirement. Whether it is uploading avatars, pictures or documents, the corresponding functions need to be implemented in the program. This article will introduce how to handle file uploads in CakePHP and some precautions. Processing uploaded files in Controller In CakePHP, uploaded files are usually processed in Cont

How to use Twig with CakePHP? How to use Twig with CakePHP? Jun 05, 2023 pm 07:51 PM

Using Twig in CakePHP is a way to separate templates and views, making the code more modular and maintainable. This article will introduce how to use Twig in CakePHP. 1. Install Twig. First install the Twig library in the project. You can use Composer to complete this task. Run the following command in the console: composerrequire "twig/twig:^2.0" This command will be displayed in the project's vendor

See all articles