初品cakephp 入门基础
首先来看一下cakephp的的执行流程(从百度百科借来的图片):
1:首先你的服务器必须支持rewrite,如果是不支持rewrite的虚拟主机的话cakephp是不能正常运行的。
2:将所有的请求定向到cakephp框架后就进入了框架的route,cakephp带有一套默认的分发规则(例如:http://……/test/test,在不做任何route配置的情况下cakephp会自动执行test_controller控制器中的test方法)。
我们可以通过配置route的方式将任何请求指向我们所希望执行的控制器和方法,配置如下(app/config/routes.php):
复制代码 代码如下:
Router::connect('/pages/*', array('controller' => 'test', 'action' => 'index'));
3:请求进入controller后cakephp会根据controller的名字去加载默认的model。例如:TestController会自动加载models下的test.php文件,接着我们就可以通过如下方法调用该model的方法了。
复制代码 代码如下:
$this->test->find('all');
查看cakephp框架的controller基类源码(cake\libs\controller\controller.php的__mergeVars方法中)
复制代码 代码如下:
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}));
}
}
}
在cakephp构造controller的时候将uses数组中的model会全部实例化。
4、5、6:是controller和model直接处理业务逻辑的一个过程,值得注意的是cakephp的model继承自AppModel,在AppModel中已经实现了一些数据库的操作方法,并且model会默认关联到数据库中的表。这一点感觉不是很好,model只是一个数据库的操作层了。
7:在进行完业务处理后,最终要数据要整合html输出到浏览器端。在cakephp的视图中包含布局文件、元素文件和模板文件,这些文件的在1.3版本中采用ctp的后缀,在controller基类里面可以修改var $ext = '.ctp';来改变模板文件的后缀。
小结:cakephp框架使用起来感觉不够灵活,model层存在局限性。而视图文件中采用的是php的语法不便于团队开发中的任务分离。在小项目中cakephp还是游刃有余的,框架提供的脚手架、核心组件和一些类可以快速方便的构建一个项目。cakephp初识,认识可能存在偏差。

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

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

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

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

Editor of the Machine Power Report: Yang Wen The wave of artificial intelligence represented by large models and AIGC has been quietly changing the way we live and work, but most people still don’t know how to use it. Therefore, we have launched the "AI in Use" column to introduce in detail how to use AI through intuitive, interesting and concise artificial intelligence use cases and stimulate everyone's thinking. We also welcome readers to submit innovative, hands-on use cases. Video link: https://mp.weixin.qq.com/s/2hX_i7li3RqdE4u016yGhQ Recently, the life vlog of a girl living alone became popular on Xiaohongshu. An illustration-style animation, coupled with a few healing words, can be easily picked up in just a few days.
