Skip to content
API control module for Yii2
PHP
Branch: master
Clone or download

Latest commit

Fetching latest commit…
Cannot retrieve the latest commit at this time.

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
commands
controllers
messages/ru-RU/app/modules
migrations
models
views
.gitignore
ApiAsset.php
CHANGELOG.md
LICENSE
Module.php
README.md
composer.json

README.md

Yii2 Github all releases Progress GitHub license GitHub release

Yii2 API

API control module

Requirements

Installation

To install the module, run the following command in the console:

$ composer require "wdmg/yii2-api"

After configure db connection, run the following command in the console:

$ php yii api/init

And select the operation you want to perform:

  1. Apply all module migrations
  2. Revert all module migrations

Migrations

In any case, you can execute the migration and create the initial data, run the following command in the console:

$ php yii migrate --migrationPath=@vendor/wdmg/yii2-api/migrations

Configure

To add a module to the project, add the following data in your configuration file:

'modules' => [
    ...
    'api' => [
        'class' => 'wdmg\api\Module',
        'routePrefix' => 'admin', // routing prefix for dashboard
        'accessTokenExpire', => 3600 // lifetime of `access_token`, `0` - unlimited
        'blockedIp' => [], // array, blocked access from IP`s
        'rateLimit' => 30, // request`s to API per minute
        'rateLimitHeaders' => false, // send HTTP-headers of rate limit
        'sendAccessToken' => true, // send access token with HTTP-headers
        'authMethods' => [ // auth methods to allow
            'basicAuth' => true,
            'bearerAuth' => true,
            'paramAuth' => true
        ],
        'authMethods' => [ // allowed API modes
            'public' => true,
            'private' => true
        ],
        'allowedApiModels' => [ // allowed API models
            'public' => [
                "wdmg\api\models\api\NewsAPI" => true,
                "wdmg\api\models\api\OptionsAPI" => false,
                "wdmg\api\models\api\PagesAPI" => false,
                "wdmg\api\models\api\UsersAPI" => false,
                ...
            ],
            'private' => [
                "wdmg\api\models\api\NewsAPI" => true,
                "wdmg\api\models\api\OptionsAPI" => true,
                "wdmg\api\models\api\PagesAPI" => true,
                "wdmg\api\models\api\UsersAPI" => true,
                ...
            ],
        ]
    ],
    ...
],

Usecase

Request to API with base auth by username and password (option authMethods['basicAuth'] must be set to true):

$ curl 'http://example.com/api/users' \
-XGET \
-H 'Accept: application/json, text/javascript, */*; q=0.01' \
-H 'Authorization: Basic YOUR_USERNAME_AND_PASSWORD'

Attention! YOUR_USERNAME_AND_PASSWORD in format username:password and has been encoded, like base64_encode('username:password') After successful authorization, the server will return X-Access-Token for use in other requests (option sendAccessToken must be set to true).

Request to API with query param access_token (option authMethods['paramAuth'] must be set to true):

$ curl 'http://example.com/api/users?access-token=YOUR_API_ACCESS_TOKEN' \
-XGET \
-H 'Accept: application/json, text/javascript, */*; q=0.01'

Request to API with bearer access_token (option authMethods['bearerAuth'] must be set to true):

$ curl 'http://example.com/api/users' \
-XGET \
-H 'Accept: application/json, text/javascript, */*; q=0.01' \
-H 'Authorization: Bearer YOUR_API_ACCESS_TOKEN'

If the access token has expired, the server will return new X-Access-Token for use in other requests (option sendAccessToken must be set to true).

Routing

Admin dashboard path by default: http://example.com/admin/api/

Path for access to API endpoint: http://example.com/api/model/module/action Also see official guideline: https://github.com/yiisoft/yii2/blob/master/docs/guide/rest-quick-start.md

Use the Module::dashboardNavItems() method of the module to generate a navigation items list for NavBar, like this:

<?php
    echo Nav::widget([
    'options' => ['class' => 'navbar-nav navbar-right'],
        'label' => 'Modules',
        'items' => [
            Yii::$app->getModule('api')->dashboardNavItems(),
            ...
        ]
    ]);
?>

Status and version [ready to use]

  • v.1.3.7 - Added pagination, up to date dependencies
  • v.1.3.6 - Added base API for Media module. Expand API for Blog module.
  • v.1.3.5 - Added base API for Blog module
  • v.1.3.4 - Added base API for Search module
  • v.1.3.3 - Fixed deprecated class declaration
You can’t perform that action at this time.