由于CakePHP现在没有很好的中文手册,所以在学习时做下笔记。刚开始不要全部理由,照着做一遍,大致能看到点效果,后面再逐步学习。我使用的环境是AppServ,安装在C:/AppServ/,根目录为C:/AppServ/www/,cakephp放在
C:/AppServ/www/cakephp/
提前注意事项:
加载rewrite模块
确保apache的模块加载:
LoadModule rewrite_module libexec/apache2/mod_rewrite.so
以及严格的目录允许被重写
Options FollowSymLinks
AllowOverride All
# Order deny,allow
# Deny from all
php开启pdo_mysql模块 (在php配置文件中将相应模块前的;或#去掉即可)
1、下载CakePHP2.3.0-beta.zip
网址:https://github.com/cakephp/cakephp/tags
2、解压后改名cakephp,放到网站的根目录
2.1 确保app/tmp目录及子目录有写权限,windows服务器可不用理会。
3、进行配置数据库连接信息和安全相关的两个配置值
3.1、数据库配置:将 app/config/database.php.default复制一份命名为database.php,主要配置用户名密码和数据库以及表前缀
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root', //mysql用户名
'password' => 'root', //mysql密码
'database' => 'test', //数据库名
'prefix' => '', //表前缀
//'encoding' => 'utf8', //字符集编码默认utf8
);
3.2、配置安全参数:将 app/config/core.php 找到以下的选项,将值替换为随机字符串
Configure::write('Security.salt', '替换后的随机字符串');
Configure::write('Security.cipherSeed', '替换后的随机数字');
4、创建数据表,并插入测试数据
CREATE TABLE posts (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(50),
body TEXT,
created DATETIME DEFAULT NULL,
modified DATETIME DEFAULT NULL);
INSERT INTO posts (title,body,created) VALUES ('The title', 'This is the post body.', NOW());
INSERT INTO posts (title,body,created) VALUES ('A title once again', 'And the post body follows.', NOW());
INSERT INTO posts (title,body,created) VALUES ('Title strikes back', 'This is really exciting! Not.', NOW());
5、创建模型,并添加自动验证规则
在app/Model/下面创建Post.php (注意命名规范,文件首字母大写),写上一个类,并加上自动验证规则
class Post extends AppModel {
//验证规则,title和body字段不允许为空
public $validate = array(
'title' => array(
'rule' => 'notEmpty'
),
'body' => array(
'rule' => 'notEmpty'
)
);
}
6、创建控制器,并写上方法
在app/Controller/下创建PostsController.php (注意命名规范,这里的命名首字母大写,并且用表的复数形式:例如peopleController.php、BooksController.php) 现在如果要访问view方法可以用 http://localhost/cakephp/posts/view
class PostsController extends AppController {
public $helpers = array('Html', 'Form');
public function index() { //查询Post表全部记录并将结果传送到视图层
$this->set('posts', $this->Post->find('all'));
}
public function view($id) { //查询一条记录,根据id
$this->Post->id = $id;
$this->set('post', $this->Post->read());
}
public function add() { //添加方法
if ($this->request->is('post')) {
$this->Post->create();
if ($this->Post->save($this->request->data)) {
$this->Session->setFlash('Your post has been saved.');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash('Unable to add your post.');
}
}
}
public function edit($id = null) { //编辑方法
$this->Post->id = $id;
if ($this->request->is('get')) {
$this->request->data = $this->Post->read();
} else {
if ($this->Post->save($this->request->data)) {
$this->Session->setFlash('Your post has been updated.');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash('Unable to update your post.');
}
}
}
public function delete($id) { //删除方法
if ($this->request->is('get')) {
throw new MethodNotAllowedException();
}
if ($this->Post->delete($id)) {
$this->Session->setFlash('The post with id: ' . $id . ' has been deleted.');
$this->redirect(array('action' => 'index'));
}
}
}
7、建立视图层
在app/View/建立目录Posts/,然后在目录下面建立index.ctp (默认的模板后缀名是ctp。 修改模板后缀在app/AppController.php文件中的class AppController extends Controller {} 类中添加一个成员属性 public $ext = '.html';) (需要修改其他的配置信息可参考核心文件:lib/Cake/Controller/Controller.php,里面有的属性都可以定制,可以在核心文件中修改,也可以在app/AppController.php中修改,建立在app中修改,因为方便框架升级)
Blog posts
Html->link('Add Post', array('action' => 'add')); ?>
继续创建浏览单条记录的view.ctp
Created:
继续创建编辑记录的模板edit.ctp
Edit Post
echo $this->Form->create('Post', array('action' => 'edit'));
echo $this->Form->input('title');
echo $this->Form->input('body', array('rows' => '3'));
echo $this->Form->input('id', array('type' => 'hidden'));
echo $this->Form->end('Save Post');
最后添加增加记录的模板add.ctp
Add Post
echo $this->Form->create('Post');
echo $this->Form->input('title');
echo $this->Form->input('body', array('rows' => '3'));
echo $this->Form->end('Save Post');
?>
8、配置路由规则,让posts控制器的index方法做为根目录的访问地址
在app/Config/routes.php中找到
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
改为: Router::connect('/', array('controller' => 'posts', 'action' => 'index'));
9、现在打开地址: http://localhost/cakephp/ 进行访问首页,并且可以对post表进行增删改查