Giter Club home page Giter Club logo

hyperf-result's Introduction

hyperf rpc 或者api 响应结果统一数据格式

该组件基于 hyperf/constants ,实现了统一返回的数据格式。

安装

composer require bailangzhan/hyperf-result

使用

<?php

namespace Bailangzhan\Result;

require_once dirname(__DIR__) . '/vendor/autoload.php';

/**
 * Class SomeClassTest
 * @package BaiLangZhan
 */
class SomeClassTest
{
    /**
     * 需要输出正确内容
     * @return array
     */
    public function success()
    {
        $result = [
            'name' => 'Bailangzhan',
        ];

        return Result::success($result);
    }

    /**
     * 需要输出错误内容时,可自定义code和消息内容
     * @return array
     */
    public function error()
    {
        return Result::error('not found');
        // return Result::error('not found', 404, []);
    }
}

//$someClass = new SomeClassTest();
//
//var_dump(json_encode($someClass->success()));
//var_dump(json_encode($someClass->error()));

// 分别输出结果如下
// string(55) "{"code":200,"message":"","data":{"name":"Bailangzhan"}}"
// string(42) "{"code":0,"message":"not found","data":[]}"

// 输出的数据格式固定 code message data

hyperf-result's People

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.