Skip to content

jefferyjob/laravel-api-code

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel Library Api Code

环境要求

laravel php

安装

composer 安装

composer require jefferyjob/laravel-api-code

添加服务

在 laravel framework 配置目录 conf 目录的 app.php 文件种,容器配置 providers 种配置IOC

Jefferyjob\LaravelApiCode\ApiCodeServiceProvider::class,

发布配置

php artisan vendor:publish --provider="Jefferyjob\LaravelApiCode\ApiCodeServiceProvider"

API数据优化

app/Exceptions/Handler.php 增加,接口在匹配以下规则的情况下,在异常抛出中以 Json 格式输出错误。

/**
 * 将异常渲染至 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 中配置数据返回的中间件

protected $middlewareGroups = [
    'api' => [
        // ... ...
        \Jefferyjob\LaravelApiCode\App\Middleware\Api::class,
        // ... ...
    ],
];

About

Api 信息返回辅助扩展包

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages