PHP code example of wandoubaba / res

1. Go to this page and download the library: Download wandoubaba/res library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

wandoubaba / res example snippets


use Wandoubaba/Res;

$res = new Res();
$res->success()
    ->setData('Hello world.');
var_dump($res);

const ERROR = 0;                // System Exception
const SUCCESS = 200;            // success
const FAILED = -1;              // 通用一般错误
const NOT_LOGED = 306;          // 自定义306表示未登录
const HEARTBEAT = 1;            // 自定义1表示心跳消息,可忽略
const NOT_FOUND = 404;          // 404错误
const INTERNAL_ERROR = 500;     // 500错误
const NOT_ALLOWED = 308;        // 自定义308表示没有权限
const NO_DATA = 407;            // 自定义407表示数据不存在
const LOGIN_FAILED = 301;       // 自定义301表示登录失败
const NO_CHANGE = 408;          // 自定义408表示无数据变化

const CODE_MESSAGES = array(
    self::ERROR => 'System Exception',
    self::SUCCESS => 'Success',
    self::FAILED => 'Failed',
    self::NOT_LOGED => 'Not Logged In',
    self::HEARTBEAT => 'Heartbeat',
    self::NOT_ALLOWED => 'Permission Denied',
    self::NOT_FOUND => 'Not Found',
    self::INTERNAL_ERROR => 'System Error',
    self::NO_DATA => 'No Data',
    self::LOGIN_FAILED => 'Login Failed',
    self::NO_CHANGE => 'No Change',
);

/* config/container.php */
$builder = new \DI\ContainerBuilder();
$builder->addDefinitions(config('dependence', []));
$builder->useAutowiring(true);
$builder->useAttributes(true);
return $builder->build();

/* config/dependence.php */
return [
    'code_messages' => app\biz\ResCode::CODE_MESSAGES,
];

/* app/utils/ResCode.php */
class ResCode
{
    const SUCCESS = 0;
    const FAILED = -1;
    const NOT_LOGED = 306;
    const TOKEN_ERROR = 309;

    const CODE_MESSAGES = [
        self::SUCCESS => '操作成功',
        self::FAILED => '操作失败',
        self::NOT_LOGED => '请先登录',
        self::TOKEN_ERROR => '令牌无效',

    ];
}

/* controller */
use Wandoubaba/Res;
use app\utils\ResCode;
use support\Container;

class TestController
{
    public function index(Request $request)
    {
        $res = Container::make(Res::class);
        $res->setCode(ResCode::TOKEN_ERROR);
        return json($res);
    }
}