├── .DS_Store ├── README.md ├── app ├── .DS_Store ├── controllers │ ├── PagesController.php │ └── TasksController.php ├── models │ └── Task.php ├── routes.php └── views │ ├── about.view.php │ ├── contact.view.php │ ├── index.view.php │ └── partials │ ├── footer.php │ ├── head.php │ └── nav.php ├── composer.json ├── config.php ├── core ├── .DS_Store ├── App.php ├── Request.php ├── Router.php ├── bootstrap.php ├── database │ ├── Connection.php │ └── QueryBuilder.php └── helpers.php ├── index.php ├── public ├── .DS_Store └── css │ └── style.css └── vendor ├── autoload.php └── composer ├── ClassLoader.php ├── LICENSE ├── autoload_classmap.php ├── autoload_files.php ├── autoload_namespaces.php ├── autoload_psr4.php ├── autoload_real.php ├── autoload_static.php └── installed.json /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhoujiping/build-your-own-php-framework/ba5c849414471f51bcb184c8cecef3ff2805b472/.DS_Store -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 论PHP框架是如何诞生的代码 2 | 3 | 文章地址: http://www.zhoujiping.com/php/build-your-own-php-framework/ 4 | 5 | ## 分支说明及文章对应代码阅读说明 6 | 7 | 一共有5个分支: 8 | 9 | ```bash 10 | 11 | `the_first_refactoring` - 对应文章中目录: 第一次代码重构 12 | `the_second_refactoring` - 对应文章中目录: 第二次代码重构 13 | `the_third_refactoring` - 对应文章中目录: 第三次代码重构 14 | `the_fourth_refactoring` - 对应文章中目录: 第四次代码重构 15 | `master` - 最后完成的代码 16 | ``` 17 | 18 | ## 查阅和切换分支 19 | 20 | ```bash 21 | # 复制代码 22 | git clone https://github.com/zhoujiping/build-your-own-php-framework.git 23 | 24 | # 切换分支 25 | git checkout the_first_refactoring 26 | ``` 27 | 28 | ## 先创建数据库 29 | 30 | 使用 Mysql 数据库,创建名为 `todolist` 的数据库,数据库中只有一个表`tasks`, 字段如下: 31 | 32 | ```bash 33 | 34 | +-------------+------------------+------+-----+---------+----------------+ 35 | | Field | Type | Null | Key | Default | Extra | 36 | +-------------+------------------+------+-----+---------+----------------+ 37 | | id | int(11) unsigned | NO | PRI | NULL | auto_increment | 38 | | description | text | NO | | NULL | | 39 | | completed | tinyint(1) | NO | | NULL | | 40 | +-------------+------------------+------+-----+---------+----------------+ 41 | ``` 42 | 43 | ## config.php 中的配置 44 | 45 | ```php 46 | 47 | return [ 48 | 'database' => [ 49 | 'name' => 'todolist', // 数据库名 50 | 'username' => 'root', // Mysql 登录用户名 51 | 'password' => 'password', // Mysql 登录密码 52 | 'connection' => 'mysql:host=127.0.0.1', 53 | 'options' => [ 54 | PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION 55 | ] 56 | ] 57 | ]; 58 | ``` 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /app/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhoujiping/build-your-own-php-framework/ba5c849414471f51bcb184c8cecef3ff2805b472/app/.DS_Store -------------------------------------------------------------------------------- /app/controllers/PagesController.php: -------------------------------------------------------------------------------- 1 | selectAll('tasks', 'Task'); 12 | 13 | return view('index', compact('tasks')); 14 | } 15 | 16 | public function store() 17 | { 18 | App::get('database')->create('tasks', [ 19 | 'description' => $_POST['description'], 20 | 'completed' => 0 21 | ]); 22 | 23 | return redirect(); 24 | } 25 | } -------------------------------------------------------------------------------- /app/models/Task.php: -------------------------------------------------------------------------------- 1 | completed; 17 | } 18 | 19 | // 设置任务已完成 20 | public function complete() 21 | { 22 | $this->completed = true; 23 | } 24 | } -------------------------------------------------------------------------------- /app/routes.php: -------------------------------------------------------------------------------- 1 | get('about', 'PagesController@about'); 4 | $router->get('contact', 'PagesController@contact'); 5 | 6 | $router->get('', 'TasksController@index'); 7 | $router->post('tasks', 'TasksController@store'); -------------------------------------------------------------------------------- /app/views/about.view.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |