Giter Club home page Giter Club logo

laravel-api-code's Introduction

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

laravel-api-code's People

Contributors

jefferyjob avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.