├── .gitignore
├── .htaccess
├── .travis.yml
├── README.md
├── app
├── controllers
│ └── ItemController.php
├── models
│ └── Item.php
└── views
│ ├── footer.php
│ ├── header.php
│ └── item
│ ├── add.php
│ ├── delete.php
│ ├── detail.php
│ ├── index.php
│ ├── manage.php
│ └── update.php
├── composer.json
├── composer.lock
├── config
└── config.php
├── fastphp
├── Fastphp.php
├── base
│ ├── Controller.php
│ ├── Model.php
│ └── View.php
└── db
│ ├── Db.php
│ └── Sql.php
├── index.php
├── phpunit.xml
├── static
└── css
│ └── main.css
└── tests
└── autoload.php
/.gitignore:
--------------------------------------------------------------------------------
1 | .idea
2 | vendor
3 |
--------------------------------------------------------------------------------
/.htaccess:
--------------------------------------------------------------------------------
1 |
2 | # 打开Rerite功能
3 | RewriteEngine On
4 |
5 | # 如果请求的是真实存在的文件或目录,直接访问
6 | RewriteCond %{REQUEST_FILENAME} !-f
7 | RewriteCond %{REQUEST_FILENAME} !-d
8 |
9 | # 如果访问的文件或目录不是真事存在,分发请求至 index.php
10 | RewriteRule . index.php
11 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: php
2 | php:
3 | - 5.4
4 |
5 | before_script:
6 | - composer install
7 |
8 | script:
9 | - phpunit -c phpunit.xml
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # FastPHP
2 |
3 | [](https://travis-ci.org/yeszao/fastphp)
4 | [](https://packagist.org/packages/yeszao/fastphp)
5 | [](https://packagist.org/packages/yeszao/fastphp)
6 | [](https://packagist.org/packages/yeszao/fastphp)
7 | [](https://packagist.org/packages/yeszao/fastphp)
8 |
9 | ## 简述
10 |
11 | **fastphp**是一款简单的PHP MVC框架,目的是方便学习《手把手编写自己的PHP MVC框架》教程的同学下载源代码,详细介绍请参考网站:http://www.awaimai.com/128.html 。
12 |
13 | 要求:
14 |
15 | * PHP 5.4.0+
16 |
17 | ## 目录说明
18 |
19 | ```
20 | project 根目录
21 | ├─app 应用目录
22 | │ ├─controllers 控制器目录
23 | │ ├─models 模块目录
24 | │ ├─views 视图目录
25 | ├─config 配置文件目录
26 | ├─fastphp 框架核心目录
27 | ├─static 静态文件目录
28 | ├─index.php 入口文件
29 | ```
30 |
31 | ## 使用
32 |
33 | ### 1.安装
34 | 主要介绍通过composer和git两种安装方法,选择其一即可。
35 |
36 | **方法1**:Composer安装(推荐)
37 | ```
38 | composer create-project yeszao/fastphp project --no-dev
39 | ```
40 | 其中,`--no-dev`表示不安装-dev依赖包(PHPUnit)。
41 |
42 | **方法2**:Github安装:
43 | ```
44 | git clone https://github.com/yeszao/fastphp.git project
45 | ```
46 | > 说明:这两个命令都会创建并将代码安装到`project`目录。
47 |
48 | ### 2. 创建数据库
49 |
50 | 在数据库中创建名为 project 的数据库,并插入两条记录,命令:
51 |
52 | ```
53 | CREATE DATABASE `project` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
54 | USE `project`;
55 |
56 | CREATE TABLE `item` (
57 | `id` int(11) NOT NULL auto_increment,
58 | `item_name` varchar(255) NOT NULL,
59 | PRIMARY KEY (`id`)
60 | ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
61 |
62 | INSERT INTO `item` VALUES(1, 'Hello World.');
63 | INSERT INTO `item` VALUES(2, 'Lets go!');
64 | ```
65 |
66 | ### 3.修改数据库配置文件
67 |
68 | 打开配置文件 config/config.php ,使之与自己的数据库匹配
69 |
70 | ```
71 | $config['db']['host'] = 'localhost';
72 | $config['db']['username'] = 'root';
73 | $config['db']['password'] = '123456';
74 | $config['db']['dbname'] = 'project';
75 | ```
76 |
77 | ### 4.配置Nginx或Apache
78 | 在Apache或Nginx中创建一个站点,把 project 设置为站点根目录(入口文件 index.php 所在的目录)。
79 |
80 | 然后设置单一入口, Apache服务器配置:
81 | ```
82 |
83 | # 打开Rerite功能
84 | RewriteEngine On
85 |
86 | # 如果请求的是真实存在的文件或目录,直接访问
87 | RewriteCond %{REQUEST_FILENAME} !-f
88 | RewriteCond %{REQUEST_FILENAME} !-d
89 |
90 | # 如果访问的文件或目录不是真事存在,分发请求至 index.php
91 | RewriteRule . index.php
92 |
93 | ```
94 | Nginx服务器配置:
95 | ```
96 | location / {
97 | # 重新向所有非真实存在的请求到index.php
98 | try_files $uri $uri/ /index.php$args;
99 | }
100 | ```
101 |
102 | ### 5.测试访问
103 |
104 | 然后访问站点域名:http://localhost/ 就可以了。
105 |
--------------------------------------------------------------------------------
/app/controllers/ItemController.php:
--------------------------------------------------------------------------------
1 | search($keyword);
16 | } else {
17 | // 查询所有内容,并按倒序排列输出
18 | // where()方法可不传入参数,或者省略
19 | $items = (new Item)->where()->order(['id DESC'])->fetchAll();
20 | }
21 |
22 | $this->assign('title', '全部条目');
23 | $this->assign('keyword', $keyword);
24 | $this->assign('items', $items);
25 | $this->render();
26 | }
27 |
28 | // 查看单条记录详情
29 | public function detail($id)
30 | {
31 | // 通过?占位符传入$id参数
32 | $item = (new Item())->where(["id = ?"], [$id])->fetch();
33 |
34 | $this->assign('title', '条目详情');
35 | $this->assign('item', $item);
36 | $this->render();
37 | }
38 |
39 | // 添加记录,测试框架DB记录创建(Create)
40 | public function add()
41 | {
42 | $data['item_name'] = $_POST['value'];
43 | $count = (new Item)->add($data);
44 |
45 | $this->assign('title', '添加成功');
46 | $this->assign('count', $count);
47 | $this->render();
48 | }
49 |
50 | // 操作管理
51 | public function manage($id = 0)
52 | {
53 | $item = array();
54 | if ($id) {
55 | // 通过名称占位符传入参数
56 | $item = (new Item())->where(["id = :id"], [':id' => $id])->fetch();
57 | }
58 |
59 | $this->assign('title', '管理条目');
60 | $this->assign('item', $item);
61 | $this->render();
62 | }
63 |
64 | // 更新记录,测试框架DB记录更新(Update)
65 | public function update()
66 | {
67 | $data = array('id' => $_POST['id'], 'item_name' => $_POST['value']);
68 | $count = (new Item)->where(['id = :id'], [':id' => $data['id']])->update($data);
69 |
70 | $this->assign('title', '修改成功');
71 | $this->assign('count', $count);
72 | $this->render();
73 | }
74 |
75 | // 删除记录,测试框架DB记录删除(Delete)
76 | public function delete($id = null)
77 | {
78 | $count = (new Item)->delete($id);
79 |
80 | $this->assign('title', '删除成功');
81 | $this->assign('count', $count);
82 | $this->render();
83 | }
84 | }
--------------------------------------------------------------------------------
/app/models/Item.php:
--------------------------------------------------------------------------------
1 | table` where `item_name` like :keyword";
30 | $sth = Db::pdo()->prepare($sql);
31 | $sth = $this->formatParam($sth, [':keyword' => "%$keyword%"]);
32 | $sth->execute();
33 |
34 | return $sth->fetchAll();
35 | }
36 | }
--------------------------------------------------------------------------------
/app/views/footer.php:
--------------------------------------------------------------------------------
1 |