Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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 信息返回辅助扩展包

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,
// ... ...
],
];
```