Coder Social home page Coder Social logo

zfwy-api's Introduction

Laravel Response API

A RESTful API package for the Laravel framework.

Installing

设置 composer.json

{
    "config": {
        "secure-http": false
    }
}

开发安装

$ composer config repositories.zfwy-api vcs https://github.com/zhijiangPan/zfwy-api.git
$ composer require zfwy/api:dev-main

如果要给这个包提交代码,为了方便开发, 如下配置

$ composer config repositories.zfwy-api path ../packages/zfwy-api
$ composer require zfwy/api:dev-main

线上环境安装

$ composer config repositories.zfwy-api vcs https://github.com/zhijiangPan/zfwy-api.git
$ composer require zfwy/api:~1.0

修改 App\Exceptions\Handler

按照如下设置

<?php

namespace App\Exceptions;

use Exception;
use Chuangke\Api\Exceptions\Helpers;

class Handler extends ExceptionHandler
{
    use Helpers;
    
    /**
     * Render an exception into an HTTP response.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Exception               $exception
     *
     * @return \Symfony\Component\HttpFoundation\Response
     *
     * @throws \Exception
     */
    public function render($request, Exception $exception)
    {
        return $this->covertRenderToApiResponse($request, $exception);
    }
}

Usage

控制器中使用

<?php

namespace App\Http\Controllers;

use Chuangke\Api\Routing\Helpers;

class UserController extends Controller
{
    use Helpers;
    
    public function index()
    {
        // 数组返回
        return $this->response->array([]);
        
        // 单条数据返回
        return $this->response->item(new UserResource($model));
        
        // 集合返回
        return $this->response->collection(UserResource::collection($models));
        
        // 分页
        return $this->response->collection(UserResource::collection(User::paginate(20)));
        
        // 添加 meta 信息
        return $this->response->collection(UserResource::collection(User::paginate(20)))
          ->meta('key', 'value');
          
        // 参数验证错误
        if ($var) {
            $this->response->errorUnprocessableEntity('密码错误');
        }
        
        // 内部错误
        if ($var) {
            $this->response->errorInternal();
        }
    }
} 

函数使用方式

<?php

api_response()->array();

License

MIT

zfwy-api's People

Contributors

zhijiangpan avatar

Watchers

 avatar  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.