esempla/yii2-rest-generator

Esempla Rest Generator

1.0.5 2018-10-30 13:40 UTC

This package is auto-updated.

Last update: 2024-04-17 22:56:30 UTC


README

Installation

The preferred way to install this extension is through composer.

Note: Check the composer.json for this extension's requirements and dependencies. Read this web tip /wiki on setting the minimum-stability settings for your application's composer.json.

Either run

$ php composer.phar require esempla/yii2-rest-generator "1.0.*"

or add

"esempla/yii2-rest-generator": "1.0.*"

to the `require` section of your composer.json file.

Migrations

The extension has been created with database table. You should execute database migrations.(PostgreSQL ® recommended)

 php yii migrate/up  --migrationPath="vendor/esempla/yii2-rest-generator/src/migrations" 

Module

The extension has been created as a module. You should configure the module with a name of restGenerator as shown below:

'modules' => [
	...
	 'restGenerator' => [
                'class' => 'restGenerator\Module',
                'params' => [
                    'modelsPath' => Yii::getAlias("@common") , //path for models scaning
                    'controllersPath' => Yii::getAlias("@backend") . "/modules/v1/controllers", //path where to save controllers
                    'controllersNS' => 'backend\modules\v1\controllers', //namespace for the new controllers
                    'auth_method'=>'Basic Auth'// Can be : Bearer Auth, Basic Auth
                ]
            ],
	...
],

`v1` ! Must be module, you can set other name, but this must be module.

Set aliases for src path(/common/main.php recommended)

'aliases' => [  
    ...  
    '@restGenerator' => '@vendor/esempla/yii2-rest-generator/src',  
],

Add in bootstrap.php in common or backend

Yii::setAlias('@restGenerator', dirname(dirname(__DIR__)) . '/vendor/esempla/rest-generator/src');  

Add RestGeneratorRoute for dynamic API routing(/backend/main.php recommended)

If adding new controllers to a module, this will set default REST rules for it.

'bootstrap' => [
    ...
    'restGenerator\components\RestGeneratorRoute'
],

Recommendations for settings:

'components' => [
    ...
    'urlManager' => [
          'class' => 'yii\web\UrlManager',
          'showScriptName' => false,   // Disable index.php
          'enablePrettyUrl' => true,  
        'rules' => [
            ...
    ],
],
'user' => [
            'class' => 'common\components\UserIdentity',
            'identityClass' => 'common\modules\User\models\User',
            'enableAutoLogin' => false,
            'enableSession' =>false
        ],

],


### Important ! set file access by example
 

sudo chown -R root:www-data backend/modules/v1/controllers


### Also remove RBAC for new controllers

'as access' => [

    'class' => AccessControl::className(),
    'allowActions' => [
        'site/*',
        'test/*',
        'v1/*',//for example
        'restGenerator/login/*'
    ]
],

<h2>Required for yii2-rest-generator:</h2>
<h4>Composer Require:</h4>

"sizeg/yii2-jwt": "dev-master"

<h4>Configure:</h4>
'components' => [
    'jwt' => [
        'class' => 'sizeg\jwt\Jwt',
        'key'   => 'secret',
    ],
      'user' => [
                'class' => 'common\components\UserIdentity',
                'identityClass' => 'common\modules\User\models\User',
                'enableAutoLogin' => false,
                'enableSession' =>false
            ],
    ]
<h4>Set common/config/params-local.php </h4>
'secret_key_api_token'=>'yor secret key',

## To acces module

> **Go to** <u> http:://domain_name/restGenerator</u>