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
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)
The last modified method will call the current controller and pass the parameters for execution
Let’s take a look at the called _invoke
function _invoke(&$controller, $params) {
$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
$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.

CakePHP是一个开源的PHPMVC框架,它广泛用于Web应用程序的开发。CakePHP具有许多功能和工具,其中包括一个强大的数据库查询构造器,用于交互性能数据库。该查询构造器允许您使用面向对象的语法执行SQL查询,而不必编写繁琐的SQL语句。本文将介绍如何使用CakePHP中的数据库查询构造器。建立数据库连接在使用数据库查询构造器之前,您首先需要在Ca

CakePHP是一个强大的PHP框架,为开发人员提供了很多有用的工具和功能。其中之一是分页,它可以帮助我们将大量数据分成几页,从而简化浏览和操作。默认情况下,CakePHP提供了一些基本的分页方法,但有时你可能需要创建一些自定义的分页方法。这篇文章将向您展示如何在CakePHP中创建自定义分页。步骤1:创建自定义分页类首先,我们需要创建一个自定义分页类。这个

在当今数字化时代,应用程序对于企业和个人来说,已经成为了不可或缺的一部分。应用程序能够让人们更加便捷地进行各种操作,而PHP和CakePHP框架则是日益流行的应用程序开发工具。在本文中,我们将讨论如何使用PHP和CakePHP框架进行应用程序开发。一、PHP是什么?PHP(外文全称为“HypertextPreprocessor”)是一种开源的服务器端脚本语

CakePHP是一种流行的PHP框架,用于开发Web应用程序。与许多其他PHP框架一样,CakePHP也提供了许多有用的功能和插件来帮助业务流程,其中包括生成PDF文件。这项任务可以使用FPDF插件轻松完成。本文将介绍如何在CakePHP中使用FPDF。FPDF是一种开源的PHP类库,用于生成PDF文件。它具有许多有用的功能,例如嵌入字体、添加图像、绘制基本

CakePHP是一个流行的PHP框架,它提供了方便的ORM(对象关系映射)功能,使得查询和更新数据库变得非常容易。本文将介绍如何在CakePHP中进行数据查询和更新。我们将从简单的查询和更新开始,逐步深入,了解如何使用条件和关联的模型来更复杂地查询和更新数据。基本查询首先,让我们看看如何进行最简单的查询。假设我们有一个名为“Users”的数据表,并且我们想要

CakePHP是一款优秀的PHP开发框架,它通过提供一系列强大的功能和工具,简化了Web应用程序的开发过程。而Guzzle是一个PHPHTTP客户端和请求库,它能够帮助开发者轻松地发送HTTP请求和访问Web服务。在本文中,我们将介绍如何在CakePHP中使用Guzzle,以便更加高效地开发Web应用程序。一、安装Guzzle首先,我们需要在CakePHP

CakePHP是一款流行的PHP开发框架,它提供了快速开发Web应用程序所需的基本功能和结构。在现代应用中,使用多个数据库连接已经成为了一个普遍的需求,例如,建立主从数据库连接或将数据分片到不同的数据库中。本文将介绍如何在CakePHP中使用多个数据库连接。CakePHP中的默认数据库连接在开始之前,让我们先了解一下CakePHP中默认的数据库连接。Cake

CakePHP是一款流行的PHP框架,它为Web开发提供了许多便利的功能。其中一个非常有用的功能是Cookie组件。在本文中,我们将介绍如何在CakePHP中使用Cookie组件来存储和检索数据。一、什么是Cookie?Cookie是一个小的数据片段,存储在网站上的用户计算机上。它可以用来存储用户喜好、登陆信息和其他相关的数据。Cookie可以被服务器和客户


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
