Repository: yeszao/fastphp
Branch: master
Commit: 99886508430c
Files: 26
Total size: 22.3 KB
Directory structure:
gitextract_2es6sksp/
├── .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
├── 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
================================================
FILE CONTENTS
================================================
================================================
FILE: .gitignore
================================================
.idea
vendor
================================================
FILE: .htaccess
================================================
# 打开Rerite功能
RewriteEngine On
# 如果请求的是真实存在的文件或目录,直接访问
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# 如果访问的文件或目录不是真事存在,分发请求至 index.php
RewriteRule . index.php
================================================
FILE: .travis.yml
================================================
language: php
php:
- 5.4
before_script:
- composer install
script:
- phpunit -c phpunit.xml
================================================
FILE: README.md
================================================
# FastPHP
[](https://travis-ci.org/yeszao/fastphp)
[](https://packagist.org/packages/yeszao/fastphp)
[](https://packagist.org/packages/yeszao/fastphp)
[](https://packagist.org/packages/yeszao/fastphp)
[](https://packagist.org/packages/yeszao/fastphp)
## 简述
**fastphp**是一款简单的PHP MVC框架,目的是方便学习《手把手编写自己的PHP MVC框架》教程的同学下载源代码,详细介绍请参考网站:http://www.awaimai.com/128.html 。
要求:
* PHP 5.4.0+
## 目录说明
```
project 根目录
├─app 应用目录
│ ├─controllers 控制器目录
│ ├─models 模块目录
│ ├─views 视图目录
├─config 配置文件目录
├─fastphp 框架核心目录
├─static 静态文件目录
├─index.php 入口文件
```
## 使用
### 1.安装
主要介绍通过composer和git两种安装方法,选择其一即可。
**方法1**:Composer安装(推荐)
```
composer create-project yeszao/fastphp project --no-dev
```
其中,`--no-dev`表示不安装-dev依赖包(PHPUnit)。
**方法2**:Github安装:
```
git clone https://github.com/yeszao/fastphp.git project
```
> 说明:这两个命令都会创建并将代码安装到`project`目录。
### 2. 创建数据库
在数据库中创建名为 project 的数据库,并插入两条记录,命令:
```
CREATE DATABASE `project` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
USE `project`;
CREATE TABLE `item` (
`id` int(11) NOT NULL auto_increment,
`item_name` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
INSERT INTO `item` VALUES(1, 'Hello World.');
INSERT INTO `item` VALUES(2, 'Lets go!');
```
### 3.修改数据库配置文件
打开配置文件 config/config.php ,使之与自己的数据库匹配
```
$config['db']['host'] = 'localhost';
$config['db']['username'] = 'root';
$config['db']['password'] = '123456';
$config['db']['dbname'] = 'project';
```
### 4.配置Nginx或Apache
在Apache或Nginx中创建一个站点,把 project 设置为站点根目录(入口文件 index.php 所在的目录)。
然后设置单一入口, Apache服务器配置:
```
# 打开Rerite功能
RewriteEngine On
# 如果请求的是真实存在的文件或目录,直接访问
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# 如果访问的文件或目录不是真事存在,分发请求至 index.php
RewriteRule . index.php
```
Nginx服务器配置:
```
location / {
# 重新向所有非真实存在的请求到index.php
try_files $uri $uri/ /index.php$args;
}
```
### 5.测试访问
然后访问站点域名:http://localhost/ 就可以了。
================================================
FILE: app/controllers/ItemController.php
================================================
search($keyword);
} else {
// 查询所有内容,并按倒序排列输出
// where()方法可不传入参数,或者省略
$items = (new Item)->where()->order(['id DESC'])->fetchAll();
}
$this->assign('title', '全部条目');
$this->assign('keyword', $keyword);
$this->assign('items', $items);
$this->render();
}
// 查看单条记录详情
public function detail($id)
{
// 通过?占位符传入$id参数
$item = (new Item())->where(["id = ?"], [$id])->fetch();
$this->assign('title', '条目详情');
$this->assign('item', $item);
$this->render();
}
// 添加记录,测试框架DB记录创建(Create)
public function add()
{
$data['item_name'] = $_POST['value'];
$count = (new Item)->add($data);
$this->assign('title', '添加成功');
$this->assign('count', $count);
$this->render();
}
// 操作管理
public function manage($id = 0)
{
$item = array();
if ($id) {
// 通过名称占位符传入参数
$item = (new Item())->where(["id = :id"], [':id' => $id])->fetch();
}
$this->assign('title', '管理条目');
$this->assign('item', $item);
$this->render();
}
// 更新记录,测试框架DB记录更新(Update)
public function update()
{
$data = array('id' => $_POST['id'], 'item_name' => $_POST['value']);
$count = (new Item)->where(['id = :id'], [':id' => $data['id']])->update($data);
$this->assign('title', '修改成功');
$this->assign('count', $count);
$this->render();
}
// 删除记录,测试框架DB记录删除(Delete)
public function delete($id = null)
{
$count = (new Item)->delete($id);
$this->assign('title', '删除成功');
$this->assign('count', $count);
$this->render();
}
}
================================================
FILE: app/models/Item.php
================================================
table` where `item_name` like :keyword";
$sth = Db::pdo()->prepare($sql);
$sth = $this->formatParam($sth, [':keyword' => "%$keyword%"]);
$sth->execute();
return $sth->fetchAll();
}
}
================================================
FILE: app/views/footer.php
================================================