<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                ## 編寫一個接口 通過編寫一個接口,讓我們快速了解框架的使用,編寫一個接口非常簡單,其實就是啟動一個命令行程序,程序中實例化一個 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' ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看