search
Homephp教程php手册初品cakephp 入门基础_php入门_脚本之家

近段时间项目挺轻松的,刚好有时间来了解一下cakephp。下面只是一己之见,初识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初识,认识可能存在偏差。

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

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

Video Face Swap

Video Face Swap

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

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

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),

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows

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.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor