Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jefferyjob/laravel-api-code
Api 信息返回辅助扩展包
https://github.com/jefferyjob/laravel-api-code
api laravel laravel-library
Last synced: 1 day ago
JSON representation
Api 信息返回辅助扩展包
- Host: GitHub
- URL: https://github.com/jefferyjob/laravel-api-code
- Owner: jefferyjob
- Created: 2022-03-18T08:25:02.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2022-03-19T03:51:00.000Z (over 2 years ago)
- Last Synced: 2024-04-22T01:16:28.486Z (7 months ago)
- Topics: api, laravel, laravel-library
- Language: PHP
- Homepage:
- Size: 36.1 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Laravel Library Api Code
## 环境要求
[![laravel](https://img.shields.io/badge/laravel-%3E8.x-red)](https://laravel.com)
[![php](https://img.shields.io/badge/php-%3E8.1-blue)](https://www.php.net)## 安装
**composer 安装**
```bash
composer require jefferyjob/laravel-api-code
```**添加服务**
在 laravel framework 配置目录 conf 目录的 app.php 文件种,容器配置 providers 种配置IOC
```php
Jefferyjob\LaravelApiCode\ApiCodeServiceProvider::class,
```**发布配置**
```bash
php artisan vendor:publish --provider="Jefferyjob\LaravelApiCode\ApiCodeServiceProvider"
```## API数据优化
在 `app/Exceptions/Handler.php` 增加,接口在匹配以下规则的情况下,在异常抛出中以 Json 格式输出错误。
```php
/**
* 将异常渲染至 HTTP 响应值中
*
* @param \Illuminate\Http\Request $request
* @param \Throwable $e
* @return \Symfony\Component\HttpFoundation\Response
*
* @throws \Throwable
*/
public function render($request, Throwable $e)
{
if ($request->is('ajax/*') || $request->is('api/*') || $request->ajax()){
return response()->json(app('ApiCode')->getExceptionContent($request, $e), 200);
} else {
return parent::render($request, $e);
}
}
```在 `app/Http/Kernel.php` 中配置数据返回的中间件
```php
protected $middlewareGroups = [
'api' => [
// ... ...
\Jefferyjob\LaravelApiCode\App\Middleware\Api::class,
// ... ...
],
];
```