|
1 | | -# BreadNote |
2 | | -BreadNote是一款在线轻量笔记编辑平台,在浏览器访问即可使用不需要本地安装,方便快捷。 |
| 1 | +# API development skeleton |
| 2 | + |
| 3 | +帮助你快速搭建 API 项目骨架,并指导你如何使用该骨架的细节,骨架默认开启了 SQL、Redis 日志,压测前请先关闭 `.env` 的 `APP_DEBUG` |
| 4 | + |
| 5 | +## 安装 |
| 6 | + |
| 7 | +> 需要先安装 [Swoole](https://wiki.swoole.com/#/environment) 或者 [WorkerMan](http://doc.workerman.net/install/requirement.html) |
| 8 | +
|
| 9 | +``` |
| 10 | +composer create-project --prefer-dist mix/api-skeleton api |
| 11 | +``` |
| 12 | + |
| 13 | +## 快速开始 |
| 14 | + |
| 15 | +启动 [cli-server](https://www.php.net/manual/zh/features.commandline.webserver.php) 开发服务 (零依赖) |
| 16 | + |
| 17 | +``` |
| 18 | +composer run-script --timeout=0 cliserver:start |
| 19 | +``` |
| 20 | + |
| 21 | +启动 Swoole 多进程服务 |
| 22 | + |
| 23 | +``` |
| 24 | +composer run-script --timeout=0 swoole:start |
| 25 | +``` |
| 26 | + |
| 27 | +启动 Swoole 协程服务 |
| 28 | + |
| 29 | +``` |
| 30 | +composer run-script --timeout=0 swooleco:start |
| 31 | +``` |
| 32 | + |
| 33 | +启动 WorkerMan 多进程服务 |
| 34 | + |
| 35 | +``` |
| 36 | +composer run-script --timeout=0 workerman:start |
| 37 | +``` |
| 38 | + |
| 39 | +## 执行脚本 |
| 40 | + |
| 41 | +- `composer run-script` 命令中的 `--timeout=0` 参数是防止 composer [执行超时](https://getcomposer.org/doc/06-config.md#process-timeout) |
| 42 | +- `composer.json` 定义了命令执行脚本,对应上面的执行命令 |
| 43 | + |
| 44 | +```json |
| 45 | +"scripts": { |
| 46 | + "cliserver:start": "php -S localhost:8000 public/index.php", |
| 47 | + "swoole:start": "php bin/swoole.php", |
| 48 | + "swooleco:start": "php bin/swooleco.php", |
| 49 | + "workerman:start": "php bin/workerman.php start", |
| 50 | + "cli:clearcache": "php bin/cli.php clearcache" |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +当然也可以直接下面这样启动,效果是一样的,但是 `scripts` 能帮你记录到底有哪些可用的命令,同时在IDE中调试更加方便。 |
| 55 | + |
| 56 | +``` |
| 57 | +php bin/swoole.php start |
| 58 | +``` |
| 59 | + |
| 60 | +## 编写一个 API 接口 |
| 61 | + |
| 62 | +首先修改根目录 `.env` 文件的数据库信息 |
| 63 | + |
| 64 | +然后在 `routes/index.php` 定义一个新的路由 |
| 65 | + |
| 66 | +```php |
| 67 | +$vega->handle('/users/{id}', [new Users(), 'index'])->methods('GET'); |
| 68 | +``` |
| 69 | + |
| 70 | +路由里使用了 `Users` 控制器,我们需要创建他 |
| 71 | + |
| 72 | +- 如何配置路由:[mix/vega](https://github.com/mix-php/vega#readme) |
| 73 | +- 如何调用数据库:[mix/database](https://github.com/mix-php/database#readme) |
| 74 | + |
| 75 | +```php |
| 76 | +<?php |
| 77 | + |
| 78 | +namespace App\Controller; |
| 79 | + |
| 80 | +use App\Container\DB; |
| 81 | +use Mix\Vega\Context; |
| 82 | + |
| 83 | +class Users |
| 84 | +{ |
| 85 | + |
| 86 | + /** |
| 87 | + * @param Context $ctx |
| 88 | + * @throws \Exception |
| 89 | + */ |
| 90 | + public function index(Context $ctx) |
| 91 | + { |
| 92 | + $row = DB::instance()->table('users')->where('id = ?', $ctx->param('id'))->first(); |
| 93 | + if (!$row) { |
| 94 | + throw new \Exception('User not found'); |
| 95 | + } |
| 96 | + $ctx->JSON(200, [ |
| 97 | + 'code' => 0, |
| 98 | + 'message' => 'ok', |
| 99 | + 'data' => $row |
| 100 | + ]); |
| 101 | + } |
| 102 | + |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +重新启动服务器后方可测试新开发的接口 |
| 107 | + |
| 108 | +> 实际开发中使用 PhpStorm 的 Run 功能,只需要点击一下重启按钮即可 |
| 109 | +
|
| 110 | +``` |
| 111 | +// 查找进程 PID |
| 112 | +ps -ef | grep swoole |
| 113 | +
|
| 114 | +// 通过 PID 停止进程 |
| 115 | +kill PID |
| 116 | +
|
| 117 | +// 重新启动进程 |
| 118 | +composer run-script swoole:start |
| 119 | +
|
| 120 | +// curl 测试 |
| 121 | +curl http://127.0.0.1:9501/users/1 |
| 122 | +``` |
| 123 | + |
| 124 | +## 使用容器中的对象 |
| 125 | + |
| 126 | +容器采用了一个简单的单例模式,你可以修改为更加适合自己的方式。 |
| 127 | + |
| 128 | +- 数据库:[mix/database](https://github.com/mix-php/database#readme) |
| 129 | + |
| 130 | +``` |
| 131 | +DB::instance() |
| 132 | +``` |
| 133 | + |
| 134 | +- Redis:[mix/redis](https://github.com/mix-php/redis#readme) |
| 135 | + |
| 136 | +``` |
| 137 | +RDS::instance() |
| 138 | +``` |
| 139 | + |
| 140 | +- 日志:[monolog/monolog](https://seldaek.github.io/monolog/doc/01-usage.html) |
| 141 | + |
| 142 | +``` |
| 143 | +Logger::instance() |
| 144 | +``` |
| 145 | + |
| 146 | +- 配置:[hassankhan/config](https://github.com/hassankhan/config#getting-values) |
| 147 | + |
| 148 | +``` |
| 149 | +Config::instance() |
| 150 | +``` |
| 151 | + |
| 152 | +## 部署 |
| 153 | + |
| 154 | +- CLI |
| 155 | + |
| 156 | +线上部署启动时,修改 `shell/server.sh` 脚本中的绝对路径和参数 |
| 157 | + |
| 158 | +``` |
| 159 | +php=/usr/local/bin/php |
| 160 | +file=/project/bin/swoole.php |
| 161 | +cmd=start |
| 162 | +numprocs=1 |
| 163 | +``` |
| 164 | + |
| 165 | +启动管理 |
| 166 | + |
| 167 | +``` |
| 168 | +sh shell/server.sh start |
| 169 | +sh shell/server.sh stop |
| 170 | +sh shell/server.sh restart |
| 171 | +``` |
| 172 | + |
| 173 | +使用 `nginx` 或者 `SLB` 代理到服务器端口即可 |
| 174 | + |
| 175 | +``` |
| 176 | +server { |
| 177 | + server_name www.domain.com; |
| 178 | + listen 80; |
| 179 | +
|
| 180 | + location / { |
| 181 | + proxy_http_version 1.1; |
| 182 | + proxy_set_header Connection "keep-alive"; |
| 183 | + proxy_set_header Host $http_host; |
| 184 | + proxy_set_header Scheme $scheme; |
| 185 | + proxy_set_header X-Real-IP $remote_addr; |
| 186 | + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; |
| 187 | + if (!-f $request_filename) { |
| 188 | + proxy_pass http://127.0.0.1:9501; |
| 189 | + } |
| 190 | + } |
| 191 | +} |
| 192 | +``` |
| 193 | + |
| 194 | +- PHP-FPM |
| 195 | + |
| 196 | +和 Laravel、ThinkPHP 部署方法完全一致,将 `public/index.php` 在 `nginx` 配置 `rewrite` 重写即可 |
| 197 | + |
| 198 | +``` |
| 199 | +server { |
| 200 | + server_name www.domain.com; |
| 201 | + listen 80; |
| 202 | + root /data/project/public; |
| 203 | + index index.html index.php; |
| 204 | +
|
| 205 | + location / { |
| 206 | + if (!-e $request_filename) { |
| 207 | + rewrite ^/(.*)$ /index.php/$1 last; |
| 208 | + } |
| 209 | + } |
| 210 | +
|
| 211 | + location ~ ^(.+\.php)(.*)$ { |
| 212 | + fastcgi_pass 127.0.0.1:9000; |
| 213 | + fastcgi_split_path_info ^(.+\.php)(.*)$; |
| 214 | + fastcgi_param PATH_INFO $fastcgi_path_info; |
| 215 | + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; |
| 216 | + include fastcgi_params; |
| 217 | + } |
| 218 | +} |
| 219 | +``` |
| 220 | + |
| 221 | +## License |
| 222 | + |
| 223 | +Apache License Version 2.0, http://www.apache.org/licenses/ |
0 commit comments