{"id":20441710,"url":"https://github.com/mix-php/api-skeleton","last_synced_at":"2025-04-12T23:32:15.254Z","repository":{"id":57017783,"uuid":"384388234","full_name":"mix-php/api-skeleton","owner":"mix-php","description":"API development skeleton","archived":false,"fork":false,"pushed_at":"2023-04-19T02:03:08.000Z","size":66,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-26T17:41:47.366Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mix-php.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-07-09T09:30:57.000Z","updated_at":"2023-04-19T12:10:16.000Z","dependencies_parsed_at":"2022-08-22T11:30:52.742Z","dependency_job_id":null,"html_url":"https://github.com/mix-php/api-skeleton","commit_stats":null,"previous_names":[],"tags_count":34,"template":true,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mix-php%2Fapi-skeleton","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mix-php%2Fapi-skeleton/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mix-php%2Fapi-skeleton/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mix-php%2Fapi-skeleton/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mix-php","download_url":"https://codeload.github.com/mix-php/api-skeleton/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248631679,"owners_count":21136562,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-15T09:34:20.591Z","updated_at":"2025-04-12T23:32:15.220Z","avatar_url":"https://github.com/mix-php.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# API development skeleton\n\n帮助你快速搭建 API 项目骨架，并指导你如何使用该骨架的细节，骨架默认开启了 SQL、Redis 日志，压测前请先关闭 `.env` 的 `APP_DEBUG`\n\n## 安装\n\n\u003e 需要先安装 [Swoole](https://wiki.swoole.com/#/environment) 或者 [WorkerMan](http://doc.workerman.net/install/requirement.html)\n\n```\ncomposer create-project --prefer-dist mix/api-skeleton api\n```\n\n## 快速开始\n\n启动 [cli-server](https://www.php.net/manual/zh/features.commandline.webserver.php) 开发服务 (零依赖)\n\n```\ncomposer run-script --timeout=0 cliserver:start\n```\n\n启动 Swoole 多进程服务\n\n```\ncomposer run-script --timeout=0 swoole:start\n```\n\n启动 Swoole 协程服务\n\n```\ncomposer run-script --timeout=0 swooleco:start\n```\n\n启动 Swow 协程服务\n\n\u003e 需自行安装 `composer require swow/swow:develop` 注意：swow 不支持 PHP 8.0 以下版本\n\n```\ncomposer run-script --timeout=0 swow:start\n```\n\n启动 WorkerMan 多进程服务\n\n```\ncomposer run-script --timeout=0 workerman:start\n```\n\n## 执行脚本\n\n- `composer run-script` 命令中的 `--timeout=0` 参数是防止 composer [执行超时](https://getcomposer.org/doc/06-config.md#process-timeout)\n- `composer.json` 定义了命令执行脚本，对应上面的执行命令\n\n```json\n\"scripts\": {\n    \"cliserver:start\": \"php -S localhost:8000 public/index.php\",\n    \"swoole:start\": \"php bin/swoole.php\",\n    \"swooleco:start\": \"php bin/swooleco.php\",\n    \"swow:start\": \"php bin/swow.php\",\n    \"workerman:start\": \"php bin/workerman.php start\",\n    \"cli:clearcache\": \"php bin/cli.php clearcache\"\n}\n```\n\n当然也可以直接下面这样启动，效果是一样的，但是 `scripts` 能帮你记录到底有哪些可用的命令，同时在IDE中调试更加方便。\n\n```\nphp bin/swoole.php start\n```\n\n## 编写一个 API 接口\n\n首先修改根目录 `.env` 文件的数据库信息\n\n然后在 `routes/index.php` 定义一个新的路由\n\n```php\n$vega-\u003ehandle('/users/{id}', [new Users(), 'index'])-\u003emethods('GET');\n```\n\n路由里使用了 `Users` 控制器，我们需要创建他\n\n- 如何配置路由：[mix/vega](https://github.com/mix-php/vega#readme)\n- 如何调用数据库：[mix/database](https://github.com/mix-php/database#readme)\n\n```php\n\u003c?php\n\nnamespace App\\Controller;\n\nuse App\\Container\\DB;\nuse Mix\\Vega\\Context;\n\nclass Users\n{\n\n    /**\n     * @param Context $ctx\n     * @throws \\Exception\n     */\n    public function index(Context $ctx)\n    {\n        $row = DB::instance()-\u003etable('users')-\u003ewhere('id = ?', $ctx-\u003eparam('id'))-\u003efirst();\n        if (!$row) {\n            throw new \\Exception('User not found');\n        }\n        $ctx-\u003eJSON(200, [\n            'code' =\u003e 0,\n            'message' =\u003e 'ok',\n            'data' =\u003e $row\n        ]);\n    }\n\n}\n```\n\n重新启动服务器后方可测试新开发的接口\n\n\u003e 实际开发中使用 PhpStorm 的 Run 功能，只需要点击一下重启按钮即可\n\n```\n// 查找进程 PID\nps -ef | grep swoole\n\n// 通过 PID 停止进程\nkill PID\n\n// 重新启动进程\ncomposer run-script swoole:start\n\n// curl 测试\ncurl http://127.0.0.1:9501/users/1\n```\n\n## 使用容器中的对象\n\n容器采用了一个简单的单例模式，你可以修改为更加适合自己的方式。\n\n- 数据库：[mix/database](https://github.com/mix-php/database#readme)\n\n```\nDB::instance()\n```\n\n- Redis：[mix/redis](https://github.com/mix-php/redis#readme)\n\n```\nRDS::instance()\n```\n\n- 日志：[monolog/monolog](https://seldaek.github.io/monolog/doc/01-usage.html)\n\n```\nLogger::instance()\n```\n\n- 配置：[hassankhan/config](https://github.com/hassankhan/config#getting-values)\n\n```\nConfig::instance()\n```\n\n## 部署\n\n- CLI\n\n线上部署启动时，修改 `shell/server.sh` 脚本中的绝对路径和参数\n\n```\nphp=/usr/local/bin/php\nfile=/project/bin/swoole.php\ncmd=start\nnumprocs=1\n```\n\n启动管理\n\n```\nsh shell/server.sh start\nsh shell/server.sh stop\nsh shell/server.sh restart\n```\n\n使用 `nginx` 或者 `SLB` 代理到服务器端口即可\n\n```\nserver {\n    server_name www.domain.com;\n    listen 80;\n\n    location / {\n        proxy_http_version 1.1;\n        proxy_set_header Connection \"keep-alive\";\n        proxy_set_header Host $http_host;\n        proxy_set_header Scheme $scheme;\n        proxy_set_header X-Real-IP $remote_addr;\n        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n        if (!-f $request_filename) {\n             proxy_pass http://127.0.0.1:9501;\n        }\n    }\n}\n```\n\n- PHP-FPM\n\n和 Laravel、ThinkPHP 部署方法完全一致，将 `public/index.php` 在 `nginx` 配置 `rewrite` 重写即可\n\n```\nserver {\n    server_name www.domain.com;\n    listen 80;\n    root /data/project/public;\n    index index.html index.php;\n\n    location / {\n        if (!-e $request_filename) {\n            rewrite ^/(.*)$ /index.php/$1 last;\n        }\n    }\n\n    location ~ ^(.+\\.php)(.*)$ {\n        fastcgi_pass 127.0.0.1:9000;\n        fastcgi_split_path_info ^(.+\\.php)(.*)$;\n        fastcgi_param PATH_INFO $fastcgi_path_info;\n        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;\n        include fastcgi_params;\n    }\n}\n```\n\n## License\n\nApache License Version 2.0, http://www.apache.org/licenses/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmix-php%2Fapi-skeleton","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmix-php%2Fapi-skeleton","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmix-php%2Fapi-skeleton/lists"}