Home >Backend Development >PHP Tutorial >cakephp first cakephp entry basics

cakephp first cakephp entry basics

WBOY
WBOYOriginal
2016-07-29 08:47:59820browse

 初品cakephp 入门基础
First, let’s take a look at the execution process of cakephp (picture borrowed from Baidu Encyclopedia):
1: First, your server must support rewrite. If it is a virtual host that does not support rewrite, cakephp will not run normally.
2: After directing all requests to the cakephp framework, you enter the framework's route. cakephp comes with a set of default distribution rules (for example: http://.../test/test, without any route configuration cakephp will automatically execute the test method in the test_controller controller).
We can direct any request to the controller and method we want to execute by configuring the route. The configuration is as follows (app/config/routes.php):

Copy the code The code is as follows:


Router: :connect('/pages/*', array('controller' => 'test', 'action' => 'index'));


3: After the request enters the controller, cakephp will go to the controller according to the name of the controller. Load the default model. For example: TestController will automatically load the test.php file under models, and then we can call the method of the model through the following method.

Copy the code The code is as follows:


$this->test->find('all');


View the controller base class source code of the cakephp framework (in the __mergeVars method of cakelibscontrollercontroller.php )

Copy the code The code is as follows:


if ($this->uses !== null && $this->uses !== false) {
$merge[] = 'uses';
}
foreach ($merge as $var) {
if (isset($appVars[$var]) && !empty($appVars[$var]) && is_array($this->{$var})) {
if ($var !== 'uses') {
$normal = Set::normalize($this->{$var});
$app = Set::normalize($appVars[$var]);
if ($app !== $normal) {
$this->{$var} = Set::merge($app, $normal);
}
} else {
$this->{$var } = array_merge($this->{$var}, array_diff($appVars[$var], $this->{$var}));
}
}
}


When cakephp constructs the controller All models in the uses array will be instantiated.
4, 5, 6: It is a process in which the controller and model directly handle business logic. It is worth noting that cakephp’s model inherits from AppModel. Some database operation methods have been implemented in AppModel, and the model will be associated with the database by default. table. This doesn't feel very good. The model is just an operation layer of the database.
7: After completing the business processing, the final data must be integrated into HTML and output to the browser. The view of cakephp contains layout files, element files and template files. These files adopt the suffix of ctp in version 1.3. In the controller base class, you can modify var $ext = '.ctp'; to change the suffix of the template file.
Summary: The cakephp framework feels not flexible enough to use, and the model layer has limitations. The syntax used in the view file is PHP, which is not convenient for task separation in team development. Cakephp is quite capable in small projects. The scaffolding, core components and some classes provided by the framework can quickly and easily build a project. I am new to cakephp, so my understanding may be biased.

The above introduces the basics of getting started with cakephp, the first version of cakephp, including the content of cakephp. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
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