CakePHP架构入门
(一)简介
本文主要介绍CakePHP架构,及怎样安装配置使用此架构入门进行开发。
(二)需求(1)了解基本PHP 代码。(2)已配置Apache+MySQL+PHP的开发环境。
(三)正文(1) CakePHP介绍
CakePHP是一个基于PHP,免费且开源的MVC框架。它相对其它一些PHP架构(如zend framework)更简单,可以使使用着更快速且不失灵活性地创建网络应用程序。因此对于普通初学者都能容易上手。
CakePHP有以下特点:
拥有既友好又活跃的社区
灵活的MIT 许可证
兼容 PHP4 和 PHP5
数据库交互运用了 CRUD
应用程序脚手架
代码自动产生(代码产生器)
MVC 体系架构
清晰,干净的高度自定义的URLs和路由请求分发器
内建validation验证
快速灵活的模板机制 (PHP syntax, with helpers)
拥有AJAX, JavaScript, HTML表单和更多的视图助手工具
邮件、Cookie、安全、会话(Session),和请求处理的组件
灵活的ACL访问控制机制
数据清理
灵活的视图缓存Caching
本地化
可在任何子目录里工作,很少甚至不需要更改任何 Apache 相关配置
(2) 配置开发
从http://cakephp.org/下载 CakePHP,当前的最新版本是:1.2.3.8166
下载后解压到服务器的根目录下,如:/wwwRoot/First_App,同时可以改变根目录直接指向First_AppAppWebRoot,即如:DocumentRoot "D:PHPwwwRootFirst_Appappwebroot"
重启Apache服务。浏览:http://localhost/
如果看到和此相同的页面,说明你的Apache服务配置正确。同时可以看到页面上的4个提示信息,黄色的是你必须配置的。
配置如下:
1。在First_AppappconfigCore.php找到 Configure::write('Security.salt', 'DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi');
将后面的键随便改成40个左右的任意字符串就可以了。
2。将文件First_Appappconfigdatabase.php.default的文件名改为:database.php,更改里面的数据库连接,可删除test的配置,最后的配置如下:
class DATABASE_CONFIG {
var $default = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => '123',
'database' => 'cake',
'prefix' => '',
);
}
3.如果First_Appapptmp不是可写,还需将改为可写。
更改后保存,再次浏览http://localhost/,你会发现上面的黄色提示全变成绿色。
4.路由配置,此才是整个配置的关键保证下面能否正确进行的关键。
路由用来映射URL和控制器的action
URL 样式的默认路由:
http://example.com/controller/action/param1/param2/param3
修改httpd.conf开启mod_rewrite
1去掉#LoadModule rewrite_module modules/mod_rewrite.so前的#号
2同时将AllowOverride none 改为AllowOverride all
如:
Options FollowSymLinks
AllowOverride all
上面是将所有网站目录都设为Allowoverride all,如果为all,apache服务将.htaccess来控制路由,设为none,将不予处理目录下.htaccess中的路由
可以指定单独目录进行路由,需在
AllowOverride all
更改httpd.conf后需重启Apache服务。
(3) 使用架构
如果配置正确后,就可以加入自己的代码。
3.1 创建数据库表
Create Table Items
(
id int (11) unsigned auto_increment,
name varchar(200),
text varchar(200),
CreateTime timestamp default current_timeStamp,
primary key (id)
)
insert into Items(name,text) values('Item 1','Item 1 content');
3.2创建Model:First_AppappmodelsItem.php
create Item extends AppModel{
var $name = 'Item';
}
?>
3.3创建Controller:First_AppappcontrollersItems_Controller.php
class ItemsController extends AppController{
var $name = 'Items';
var $scaffold;
}
?>
3.4浏览:http://localhost/Items,后的结果如下:

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

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.

SublimeText3 Chinese version
Chinese version, very easy to use

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Zend Studio 13.0.1
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
