2017 © Pedro Peláez
 

yii2-extension yii2-json-api-controller

Yii2 simple json base controller.

image

rgen3/yii2-json-api-controller

Yii2 simple json base controller.

  • Thursday, May 24, 2018
  • by rgen3
  • Repository
  • 1 Watchers
  • 0 Stars
  • 4 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

yii2-json-api-controller

Provides a possibility to work with json requests with friendly request validation using native yii2 model rules, (*1)

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, (*2)

Usage example

Create a request folder and a request class, (*3)

<?php

namespace app\requests;

use yii\base\Model;

class MyRequest extends Model 
{
    public $exampleVar;

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

Create a controller, (*4)

<?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, (*5)

    {
      "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, (*6)

Calling actionError you'll get, (*7)

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

Executing actionThrows returns, (*8)

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

  ]
}

If you have unhandled exception as in actionAnotherException you'll get, (*9)

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

The Versions

24/05 2018

dev-master

9999999-dev

Yii2 simple json base controller.

  Sources   Download

MIT

by Alexander Tanaev

extension json yii2 controller

24/05 2018

0.0.2

0.0.2.0

Yii2 simple json base controller.

  Sources   Download

MIT

by Alexander Tanaev

extension json yii2 controller

31/03 2018

0.0.1

0.0.1.0

Yii2 simple json base controller.

  Sources   Download

MIT

by Alexander Tanaev

extension json yii2 controller

31/03 2018

dev-add-license-1

dev-add-license-1

Yii2 simple json base controller.

  Sources   Download

MIT

by Alexander Tanaev

extension json yii2 controller