通知短信+运营短信,5秒速达,支持群发助手一键发送🚀高效触达和通知客户 广告
## 编写一个接口 通过编写一个接口,让我们快速了解框架的使用,编写一个接口非常简单,其实就是启动一个命令行程序,程序中实例化一个 Router (包含路由),然后启动一个 Http Server 。 ## 创建用户的接口 接口信息如下: - URL: http://www.domian.com/v2/user/create - Method: POST ## 配置 本实例需要使用到数据库,而数据库信息保存在 `.env` 文件中,因此需要修改为正确的数据库信息。 - 修改的文件路径: [.env#L4](https://github.com/mix-php/mix-skeleton/blob/master/.env#L4) ~~~ # DATABASE DATABASE_DSN='mysql:host=127.0.0.1;port=3306;charset=utf8;dbname=test' DATABASE_USERNAME=root DATABASE_PASSWORD=123456 ~~~ ## 路由 首先我们需要定义路由,V2.2 我们默认建议采用基于 FastRoute 的路由组件,在 [路由依赖配置](https://github.com/mix-php/mix-skeleton/blob/master/manifest/beans/route.php#L8) 中指定了路由配置的加载文件为 `routes/api.php`,我们只需增加一个接口的路由配置即可。 - 修改的文件路径: [routes/api.php](https://github.com/mix-php/mix-skeleton/blob/master/routes/api.php) 增加以下路由配置: ~~~ $collector->group('/v2', function (Mix\FastRoute\RouteCollector $collector) { $collector->post('/user/create',[\App\Api\Controllers\UserController::class, 'create']); } ); ~~~ 上面的代码配置了 `/v2/user/create` 将会调用 `[\App\Api\Controllers\UserController::class, 'create']` 方法。 ## 控制器 因此我们需要创建一个 `\App\Api\Controllers\UserController::class` 控制器和一个 `create` 方法。 - 文件路径:`app/Api/Controllers/UserController.php` ~~~php <?php namespace App\Api\Controllers; use App\Common\Helpers\ResponseHelper; use App\Api\Forms\UserForm; use App\Api\Models\UserModel; use Mix\Http\Message\Response; use Mix\Http\Message\ServerRequest; /** * Class UserController * @package App\Api\Controllers * @author liu,jian <coder.keda@gmail.com> */ class UserController { /** * Create * @param ServerRequest $request * @param Response $response * @return Response */ public function create(ServerRequest $request, Response $response) { // 使用表单验证器 $form = new UserForm($request->getAttributes()); $form->setScenario('create'); if (!$form->validate()) { $content = ['code' => 1, 'message' => 'FAILED', 'data' => $form->getErrors()]; return ResponseHelper::json($response, $content); } // 执行保存数据库 (new UserModel())->add($form); // 响应 $content = ['code' => 0, 'message' => 'OK']; return ResponseHelper::json($response, $content); } } ~~~ 骨架中提供了 `StartCommand.php` 启动命令,里面包含: - Server 启动/停止:[StartCommand.php#L70](https://github.com/mix-php/mix-skeleton/blob/master/app/Api/Commands/StartCommand.php#L70) - 路由传入到 Server 中去执行:[StartCommand.php#L82](https://github.com/mix-php/mix-skeleton/blob/master/app/Api/Commands/StartCommand.php#L82) - 在命令配置中配置 `StartCommand::class` 启动命令:[manifest/commands/api.php#L5](https://github.com/mix-php/mix-skeleton/blob/master/manifest/commands/api.php#L5) 控制器包含了: - 控制器中创建了 `UserForm` 表单,这个表单是一个验证器类,负责验证数据的有效性。 - 然后通过 `UserModel` 数据模型调用数据库保存数据,这里直接传入表单是一种非常优雅的方式。 - 最后通过 `ResponseHelper` 将返回 `json` 类型的结果给客户端。 ## 运行服务器 和 PHP-FPM 不同 Mix 是命令行程序,需要在 cli 中启动: ~~~ MacBookPro:mix-skeleton liujian$ php bin/mix.php api --host=0.0.0.0 ____ ______ ___ _____ ___ _____ / /_ _____ / __ `__ \/ /\ \/ /__ / __ \/ __ \/ __ \ / / / / / / / /\ \/ _ / /_/ / / / / /_/ / /_/ /_/ /_/_/ /_/\_\ / .___/_/ /_/ .___/ /_/ /_/ Server Name: mix-api System Name: darwin PHP Version: 7.3.12 Swoole Version: 4.5.1 Framework Version: 2.2.5 Listen Addr: 0.0.0.0 Listen Port: 9502 [2020-06-29 14:05:19] API.INFO: [StartCommand.php:81] Server start ~~~ ## 测试 现在你可以使用 `curl`、`postman` 等工具来测试这个接口了: ~~~shell curl --location --request POST 'http://127.0.0.1:9502/v2/user/create' \ --header 'Content-Type: application/x-www-form-urlencoded' \ --data-urlencode 'name=xiaoming' ~~~