PHP code example of rgen3 / yii2-json-api-controller
1. Go to this page and download the library: Download rgen3/yii2-json-api-controller 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/ */
rgen3 / yii2-json-api-controller example snippets
namespace app\requests;
use yii\base\Model;
class MyRequest extends Model
{
public $exampleVar;
public function rules()
{
return [
['exampleVar', '
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');
}
}