2017 © Pedro Peláez
 

library yii2-gii-rest

This is a resutful API creator for yii2

image

graychen/yii2-gii-rest

This is a resutful API creator for yii2

  • Friday, July 20, 2018
  • by Graychen
  • Repository
  • 2 Watchers
  • 1 Stars
  • 108 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 5 Versions
  • 0 % Grown

The README.md

yii2-gii-rest

Yii2 Code generator for REST api, (*1)

Usage

Config gii modules in your application like this in backend\config\main-local.php, (*2)

<?php

$config = [
    'components' => [
        'request' => [
            // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
            'cookieValidationKey' => '7Zahf41C4AMjxPsFuKs7m7rDwXrb-vNL',
        ],
    ],
];

if (!YII_ENV_TEST) {
    // configuration adjustments for 'dev' environment
    $config['bootstrap'][] = 'debug';
    $config['modules']['debug'] = [
        'class' => 'yii\debug\Module',
        'allowedIPs' => ['*'],
    ];

    $config['bootstrap'][] = 'gii';
    $config['modules']['gii'] = [
        'class' => 'yii\gii\Module',
        'allowedIPs' => ['*'],
        'generators' => [
            'api-rest' => [
                'class' => 'graychen\yii2\gii\rest\Generator',
                'templates' => [
                    'rest' => 'graychen\yii2\gii\rest\default'
                ]
            ]
        ]
    ];
}

return $config;

Screen Shoot

, (*3)

Code Demo

Here is the Controller generated via the User model:, (*4)

<?php

namespace api\modules\v1\client;

use Yii;
use common\models\User;
use yii\data\ActiveDataProvider;
use yii\rest\ActiveController;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * UserController implements the CRUD actions for User model.
 */
class UserController extends ActiveController
{
    public $modelClass = 'common\models\User';

    public function actions()
    {
       return [];
    }

    /**
     * Lists all User models.
     * @return mixed
     */
    public function actionIndex()
    {
        $dataProvider = new ActiveDataProvider([
            'query' => User::find(),
        ]);

        return $dataProvider;
    }

    /**
     * Displays a single User model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionView($id)
    {
        return $this->findModel($id),
    }

    /**
     * Creates a new User model.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new User();

        if ($model->load(Yii::$app->getRequest()->getBodyParams(), '') && $model->save()) {
          $response = Yii::$app->getResponse();
          $response->setStatusCode(201);
        } elseif (!$model->hasErrors()) {
          throw new yii\web\ServerErrorHttpException('Failed to create the object for unknown reason.');
        }
        return $model;
    }

    /**
     * Updates an existing User model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            Yii::$app->response->setStatusCode(200);
        } elseif (!$model->hasErrors()) {
            throw new ServerErrorHttpException('Failed to update the object for unknown reason.');
        }
            return $model;
     }

    /**
     * Deletes an existing User model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionDelete($id)
    {
        $model = new User();
        if ($this->findModel($id)->delete()) {
            Yii::$app->getResponse()->setStatusCode(204);
        } elseif (!$model->hasErrors()) {
            throw new ServerErrorHttpException('Failed to delete the object for unknown reason.');
        }
        return $model;
    }

    /**
     * Finds the User model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return User the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = User::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('The requested User does not exist.');
    }
}

The Versions

20/07 2018

dev-master

9999999-dev

This is a resutful API creator for yii2

  Sources   Download

MIT

The Requires

 

The Development Requires

by 陈家辉

20/07 2018

v1.0.1

1.0.1.0

This is a resutful API creator for yii2

  Sources   Download

MIT

The Requires

 

The Development Requires

by 陈家辉

19/07 2018

v1.0.0

1.0.0.0

This is a resutful API creator for yii2

  Sources   Download

MIT

The Requires

 

The Development Requires

by 陈家辉

14/06 2018

dev-develop

dev-develop

This is a resutful API creator for yii2

  Sources   Download

MIT

The Requires

 

The Development Requires

by 陈家辉

12/06 2018

dev-feature-composer

dev-feature-composer

This is a resutful API creator for yii2

  Sources   Download

MIT

The Requires

 

The Development Requires

by 陈家辉