定制 rgen3/yii2-json-api-controller 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

rgen3/yii2-json-api-controller

Composer 安装命令:

composer require rgen3/yii2-json-api-controller

包简介

Yii2 simple json base controller.

README 文档

README

Provides a possibility to work with json requests with friendly request validation using native yii2 model rules

Installation

composer require rgen3/yii2-json-api-controller

NB

You can use this controller with simple json client https://github.com/rgen3/restapi-core-client

Usage example

Create a request folder and a request class

<?php

namespace app\requests;

use yii\base\Model;

class MyRequest extends Model 
{
    public $exampleVar;

    public function rules()
    {
        return [
            ['exampleVar', 'required'],
        ];
    }
}

Create a controller

<?php

use rgen3\controller\json\BaseController;
use app\requests\MyRequest;


class SiteController extends BaseController
{
    // Note sending data to autoload for the request you should use key named `data` as wrapper
    // for you json request
    //
    // i.e. here as input datum you have to use
    // "
    //  {"data" : {"exampleVar" : "myValue" }}
    // "
    public function actionIndex(MyRequest $request)
    {
        // return any success response
        return $this->success([
            'theValue' => $request->exampleVar,
        ]);
    }

    // Also you can any typehints to provide a native yii2 behaviour
    public function actionError(int $anyVar = 0)
    {
        // $anyVar will contains your value or zero
        return $this->error(['Error data']);
    }

    // Also you can leave input params empty
    public function actionThrows()
    {
        // Throw any exception you want
        throw new yii\web\BadRequestHttpException('The exception');
    }
    
    public function actionAnotherException()
    {
        throw new \Exception('Not http exception');
    }
}

When you call actionIndex you'll get

    {
      "request-id": null,
      "status-code": 200,
      "status-text": "OK",
      "status": "success",
      "data": {
        "theValue": "your value"
      }
    }

note that you should generate request-id and set it to X-Request-Id http header

Calling actionError you'll get

{
  "request-id": null,
  "status-code": 200,
  "status-text": "OK",
  "status": "error",
  "data": [
    "Error class"
  ]
}

Executing actionThrows returns

{
  "request-id": null,
  "status-code": 400,
  "status-text": "Bad Request",
  "status": "error",
  "data": [
    
  ]
}

If you have unhandled exception as in actionAnotherException you'll get

{
  "request-id": null,
  "status-code": 500,
  "status-text": "Internal Server Error",
  "status": "error",
  "data": [
    "Not http exception"
  ]
}

统计信息

  • 总下载量: 19
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-03-30

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固