{"id":17715768,"url":"https://github.com/caikeal/standard-output","last_synced_at":"2026-05-03T06:36:22.025Z","repository":{"id":57005240,"uuid":"82049331","full_name":"caikeal/standard-output","owner":"caikeal","description":"standard-output for laravel","archived":false,"fork":false,"pushed_at":"2017-03-10T08:47:06.000Z","size":28,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-14T02:03:11.340Z","etag":null,"topics":["laravel","laravel-package","response","restful-api","restful-api-php"],"latest_commit_sha":null,"homepage":null,"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/caikeal.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":"2017-02-15T10:36:39.000Z","updated_at":"2017-03-10T08:44:03.000Z","dependencies_parsed_at":"2022-08-21T13:50:50.352Z","dependency_job_id":null,"html_url":"https://github.com/caikeal/standard-output","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"purl":"pkg:github/caikeal/standard-output","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/caikeal%2Fstandard-output","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/caikeal%2Fstandard-output/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/caikeal%2Fstandard-output/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/caikeal%2Fstandard-output/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/caikeal","download_url":"https://codeload.github.com/caikeal/standard-output/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/caikeal%2Fstandard-output/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32560867,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-03T03:21:47.309Z","status":"ssl_error","status_checked_at":"2026-05-03T03:21:43.884Z","response_time":103,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["laravel","laravel-package","response","restful-api","restful-api-php"],"created_at":"2024-10-25T12:23:45.004Z","updated_at":"2026-05-03T06:36:22.010Z","avatar_url":"https://github.com/caikeal.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 关于`Laravel`项目规范`Api`输出和错误提示（测试版本）\n\n# 安装\n建议使用Laravel \u003e= 5.1.* , PHP \u003e= 5.6\n\n`composer require keal/laravel-output`\n\n# 使用\n## 注册ServiceProvider\n在`app.php`中加入`Caikeal\\Output\\Provider\\OutputServiceProvider::class`\n\n## 引入错误处理\n需要在`Exceptions/Handler.php`中引入`Caikeal\\Output\\Exceptions\\HandlerTrait`该方法，\n然后在改写下`render`方法，如下：\n```\n    public function render($request, Exception $exception)\n    {\n        if ($request-\u003eexpectsJson()) {\n            return $this-\u003ehandlerApi($exception);\n        }\n\n        return parent::render($request, $exception);\n    }\n```\n## 引入返回值基类\n在所有`Controller`的基类中引入该方法，如下：\n```\n    protected function response()\n    {\n        return app('output.response');\n    }\n```\n\n## 返回方法\n在返回时可以用该方法代替response返回：`$this-\u003ereponse()`的方法\n所有方法如下：\n+ 正确值返回：\n    - model层，且带有transformer验证的单列数据：`$this-\u003eresponse()-\u003eitem([Model], [Transformer]);`\n    - model层，且带有transformer验证的多列数据：`$this-\u003eresponse()-\u003ecollection([Model], [Transformer]);`\n    - model层，且带有transformer验证的分页数据：`$this-\u003eresponse()-\u003epaginator([Model], [Transformer]);`\n    - 任意：`$this-\u003eresponse()-\u003earray([Model/Array]);`\n    \n    返回值样式：\n    ```\n      {\n        \"data\": {\n          \"name\": \"keal\"\n        },\n        \"meta\": {\n          \"type\": \"seller\"\n        }\n      }\n    ```\n\n+ 错误值返回：\n    - 404错误：`$this-\u003eresponse()-\u003eerrorNotFound([$message], [$code])`\n    - 400错误：`$this-\u003eresponse()-\u003eerrorBadRequest([$message], [$code])`\n    - 403错误：`$this-\u003eresponse()-\u003eerrorForbidden([$message], [$code])`\n    - 500错误：`$this-\u003eresponse()-\u003eerrorInternal([$message], [$code])`\n    - 401错误：`$this-\u003eresponse()-\u003eerrorUnauthorized([$message], [$code])`\n    - 405错误：`$this-\u003eresponse()-\u003eerrorMethodNotAllowed([$message], [$code])`\n    - 任意错误：`$this-\u003eresponse()-\u003eerror([$message], [$statusCode], [$code])`\n    \n    返回值样式：\n    ```\n      {\n        \"code\": \"401000\",\n        \"status_code\": \"401\",\n        \"message\": \"Wrong.\"\n      }\n    ```\n \n## `Transformer`的数据格式转换\n在所有`Transformer`类中必须继承`League\\Fractal\\TransformerAbstract`，使用如下：\n```\n    namespace App\\Transformers;\n    \n    use App\\User;\n    use League\\Fractal\\TransformerAbstract;\n    \n    class UserTransformer extends TransformerAbstract\n    {\n        public function transform(User $data){\n            return [\n                'id'=\u003eintval($data['id']),\n                'name'=\u003eucfirst($data['name'])\n            ];\n        }\n    }\n```\n\n## `Artsian`创建`Transformer`类\n创建Transformer：`php artisan make:transformer User`\n\n附带Model名：`php artisan make:transformer User -m User`\n\n或者`php artisan make:transformer User --model=User`\n\n或者`php artisan make:transformer User --model=Model/User` 带位置，命令会自动转化成空间名，并在开头添加`App`\n\n# 感谢\n主要借鉴和引用了[Dingo/api](https://github.com/dingo/api.git)和[league/fractal](https://github.com/thephpleague/fractal.git)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcaikeal%2Fstandard-output","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcaikeal%2Fstandard-output","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcaikeal%2Fstandard-output/lists"}