2017 © Pedro Peláez
 

project yii2-app-restapi

Yii 2 Rest API Project Template

image

dersonsena/yii2-app-restapi

Yii 2 Rest API Project Template

  • Monday, April 30, 2018
  • by dersonsena
  • Repository
  • 1 Watchers
  • 1 Stars
  • 16 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 572 Forks
  • 0 Open issues
  • 21 Versions
  • 60 % Grown

The README.md

, (*1)

Yii 2 API Rest Project Template


Yii 2 API Rest Project Template is a skeleton Yii 2 application best for rapidly creating API Rest projects., (*2)

DIRECTORY STRUCTURE

The directory structure is similar to the Basic template added in the Advanced feature, which is the generation of the *-local.php, web/index.php files, and the yii script according to your environment, (*3)

  + assets/             contains assets definition
  + components/         containes the application components
  + commands/           contains console commands (controllers)
  - config/             contains application configurations
    * routes/           contains the routes configurations files
  + environments        contains environments templates files (see the advanced template)
  + mail/               contains view files for e-mails
  + migrations/         contains the migrations scripts
  + models/             contains model classes
  + modules/            contains de application modules
  + runtime/            contains files generated during runtime
  + tests/              contains various tests for the basic application
  + vendor/             contains dependent 3rd-party packages
  + web/                contains the entry script and Web resources

REQUIREMENTS

The minimum requirement by this project template that your Web server supports PHP 5.4.0., (*4)

INSTALLATION

Install via Composer

If you do not have Composer, you may install it by following the instructions at getcomposer.org., (*5)

You can then install this project template using the following command:, (*6)

php composer create-project --stability=dev dersonsena/yii2-app-restapi

Now you should be able to access the application through the following URL, assuming basic is the directory directly under the Web root., (*7)

http://localhost/basic/web/

Install with Docker

Update your vendor packages, (*8)

docker-compose run --rm php composer update --prefer-dist

Run the installation triggers (creating cookie validation code), (*9)

docker-compose run --rm php composer install

Start the container, (*10)

docker-compose up -d

You can then access the application through the following URL:, (*11)

http://127.0.0.1:8000

See more details in: https://hub.docker.com/_/mysql, (*12)

NOTES: - Minimum required Docker engine version 17.04 for development (see Performance tuning for volume mounts) - The default configuration uses a host-volume in your home directory .docker-composer for composer caches, (*13)

CONFIGURATION

Environment Files

Just like in the Advanced template, to generate the files according to your work environment you need to execute the init script and then choose your environment: Development or Production, (*14)

This script will create the files:, (*15)

  /yii
  /web/index.php
  /config/console-local.php
  /config/web-local.php

NOTES:, (*16)

See more details for use in the Advanced template documentation., (*17)

Database

Edit the file config/web-local.php with real data, for example:, (*18)

'components' => [
    'db' => [
        'class' => 'yii\db\Connection',
        'dsn' => 'mysql:host=127.0.0.1;dbname=your_database_name',
        'username' => 'root',
        'password' => '',
        'charset' => 'utf8',

        // Schema cache options (for production environment)
        //'enableSchemaCache' => true,
        //'schemaCacheDuration' => 60,
        //'schemaCache' => 'cache',
    ],
],

NOTES: - Yii won't create the database for you, this has to be done manually before you can access it. - You also need to configure the database guidelines in config/console-local.php for CLI scripts., (*19)

Migration

Run the migration script to create the users table with the following command:, (*20)

php ./yii migrate

The User.php model has already been generated by the GII, but a few things have been added. To access it, simply open the models/User.php file, (*21)

NOTES:, (*22)

All models MUST extend the app\components\ModelBase class. See it for more implementation details., (*23)

API Routes

All your API routes must be within config/routes. The following code is the already configured route of users:, (*24)

return [
    [
        'class' => 'yii\rest\UrlRule',
        'controller' => 'v1/system/users',
        'tokens' => ['{id}' => '<id:[\\w-]+>'],
        'pluralize' => false,
    ]
];

Accessing Users Web Service

To access the list of users through your REST API, simply access the address below in your browser or in external applications such as Postman:, (*25)

GET http://<YOUR_BASE_URL>/v1/system/users

When accessing this address you should see a list with only one user similar to JSON below:, (*26)

[
    {
        "id": 1,
        "username": "admin",
        "password_reset_token": null,
        "email": "admin@admin.com.br",
        "status": 1,
        "created_at": "09/04/2018 04:18:52",
        "updated_at": "09/04/2018 04:18:52"
    }
]

See more details in RESTful Web Services Guide., (*27)

Authentication

The authentication used is HttpBearerAuth, where a Token with JWT is passed for user authentication in its API. Initially it is inactive. To activate it is quite simple, just go in the app\components\BaseController class and uncomment the lines:, (*28)

$behaviors['authenticator'] = [
    'class' => HttpBearerAuth::className(),
        'except' => $excepts
    ];

    $behaviors['jsxValidator'] = [
        'class' => JsxValidator::className(),
        'except' => $excepts
    ];
]

When uncommenting these lines and trying to rerun the list of users, you should return a message as below:, (*29)

{
    "name": "Unauthorized",
    "message": "Your request was made with invalid credentials.",
    "code": 0,
    "status": 401,
    "type": "yii\\web\\UnauthorizedHttpException"
}

NOTES: If you want some action to be open, that is, do not authenticate to the API, just add inside the $excepts array., (*30)

For more details on REST authentication with Yii, see the Official Framework Documentation., (*31)

The Versions

30/04 2018

dev-master

9999999-dev http://www.yiiframework.com/

Yii 2 Rest API Project Template

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

api framework rest yii2 project template restapi yii2 api yii2 api template

30/04 2018

1.2.0

1.2.0.0 http://www.yiiframework.com/

Yii 2 Rest API Project Template

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

api framework rest yii2 project template restapi yii2 api yii2 api template

10/04 2018

1.1.0

1.1.0.0 http://www.yiiframework.com/

Yii 2 Rest API Project Template

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

api framework rest yii2 project template restapi yii2 api yii2 api template

09/04 2018

1.0.0

1.0.0.0 http://www.yiiframework.com/

Yii 2 Rest API Project Template

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

api framework rest yii2 project template restapi yii2 api yii2 api template

11/07 2016

2.0.9

2.0.9.0 http://www.yiiframework.com/

Yii 2 Basic Project Template

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

framework yii2 project template basic

28/04 2016

2.0.8

2.0.8.0 http://www.yiiframework.com/

Yii 2 Basic Project Template

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

framework yii2 project template basic

14/02 2016

2.0.7

2.0.7.0 http://www.yiiframework.com/

Yii 2 Basic Project Template

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

framework yii2 project template basic

06/08 2015

2.0.6

2.0.6.0 http://www.yiiframework.com/

Yii 2 Basic Project Template

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

framework yii2 project template basic

11/05 2015

2.0.4

2.0.4.0 http://www.yiiframework.com/

Yii 2 Basic Project Template

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

framework yii2 project template basic

01/03 2015

2.0.3

2.0.3.0 http://www.yiiframework.com/

Yii 2 Basic Application Template

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

framework application template yii2 basic

11/01 2015

2.0.2

2.0.2.0 http://www.yiiframework.com/

Yii 2 Basic Application Template

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

framework application template yii2 basic

07/12 2014

2.0.1

2.0.1.0 http://www.yiiframework.com/

Yii 2 Basic Application Template

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

framework application template yii2 basic

12/10 2014

2.0.0

2.0.0.0 http://www.yiiframework.com/

Yii 2 Basic Application Template

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

framework application template yii2 basic

28/09 2014

2.0.0-rc

2.0.0.0-RC http://www.yiiframework.com/

Yii 2 Basic Application Template

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

framework application template yii2 basic

13/04 2014

2.0.0-beta

2.0.0.0-beta http://www.yiiframework.com/

Yii 2 Basic Application Template

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

framework application template yii2 basic

01/12 2013

2.0.0-alpha

2.0.0.0-alpha http://www.yiiframework.com/

Yii 2 Basic Application Template

  Sources   Download

BSD-3-Clause

The Requires

 

framework application template yii basic