Home Backend Development PHP Tutorial 初品cakephp 入门基础_PHP

初品cakephp 入门基础_PHP

Jun 01, 2016 pm 12:12 PM
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 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

Video Face Swap

Video Face Swap

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

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 Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

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

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 Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

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

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

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 to use TCPDF with CakePHP? How to use TCPDF with CakePHP? Jun 05, 2023 pm 12:40 PM

CakePHP is a very popular PHP framework that provides many convenient methods for web application development. TCPDF is a very commonly used PDF generation library when we need to generate PDF files in applications. This article will introduce how to use TCPDF in CakePHP. Install TCPDF First, we need to install TCPDF in our CakePHP project. This can be done in a few ways, such as manually copying the TCPDF to the project's v

See all articles