nikxphreaker / yii2-hiring-backend
Yii 2 RESTful API
Requires
- yiisoft/yii2: ~2.0.14
README
Yii 2 RESTful API
++++++++++++++++++++++++++++++++++ Yii2 RESTful API ++++++++++++++++++++++++++++++++++ This API using template from Yii-api-template. This is a a REST API TEMPLATE with Yii2. This template use Yii2-Micro approach so it will be lightweight and easy to deploy.
Installation
Install composer.
Install with git
git clone https://github.com/nikophreaker/yii2-hiring-backend.git [app-name]
To directory [app-name]
composer install
Setup Database
Setup database configuration from config/db.php
.
<?php return [ 'components' => [ 'db' => [ 'class' => 'yii\db\Connection', 'dsn' => 'mysql:host=localhost;dbname=your_db_name', 'username' => 'root', 'password' => '', 'charset' => 'utf8', ], ], ];
Import database hiring-backend.sql
Running API
yii serve
Directory Structure
Since this template use MicroFramework approach, directory structure might be a little bit different from Yii2.
config/ contains application configurations
controllers/ contains Web controller classes
models/ contains model classes
modules/ contains your rest-api versioning (based on modules)
vendor/ contains dependent 3rd-party packages
web/ contains the entry script and Web resources
Test Using Postman
Download Postman.
Auth Scenario
This template already have basic endpoint that you can use to start your REST-API. Such as:
This template use modules as versioning pattern. Every version of API saved in a module. This template already have v1 module, so it means if consumer want to use v1 API, it can access https://localhost:8080/v1/endpoint
.
API Scenario
Creating User
Login User
Don't forget to save your token
Setting token
Change type authorization to Bearer Token and place your token to Token field
Create Session
Input key and values like an image
Edit Session
Set id on params from exist data Session and edit it throught `x-www-form-urlencoded`
Delete Session
Set id on params from exist data Session
Viewing Detail Session
You need token if you using v1 endpoint
List Session
You need token if you using v1 endpointSupported Authentication
This template support 3 most used authentication. (Actually it's not me who make it, Yii2 already support it all :D ).
- HTTP Basic Auth: the access token is sent as the username. This should only be used when an access token can be safely stored on the API consumer side. For example, the API consumer is a program running on a server.
- Query parameter: the access token is sent as a query parameter in the API URL, e.g., https://example.com/users?access-token=xxxxxxxx. Because most Web servers will keep query parameters in server logs, this approach should be mainly used to serve JSONP requests which cannot use HTTP headers to send access tokens.
- OAuth 2: the access token is obtained by the consumer from an authorization server and sent to the API server via HTTP Bearer Tokens, according to the OAuth2 protocol.
Access Token Management
This application manage token via cookie without storing to table or data in database. Access Token have certain expiration based on $tokenExpiration value. Default Token Expiration are in seconds.
public $tokenExpiration = 60 * 24 * 365; // in seconds
This cookies stored to your computer
if you want to delete all of this, you can using logout endpoint like this
API versioning
This template give you versioning scenario based on module application. In Yii2 a module are self-contained software units that consist of model, views, controllers and other supporting components. This template already have v1 module, it means all of endpoint for API v1 created in this module. When you publish a new API version (that break backward compatibility / BBC), you can create a new module. For more information create a module, you can visit this Yii2 Guide on Creating Module.