How to use Yii2 as API consumer (No DB connection)

Hello All,

Good Day!

I am developing a Yii2 Application. This will be pure API consumer application. No database connectivity.
Login API accepts user name and password, and returns oAuth Token. This token is used for subsequent operations.

  1. What should be the standards for this kind of implementations?
  2. What is the correct way to save the login session?
  3. Which Library i can make use of for the API requests?

Hello there,

Yii provides a package for http (yii2-httpclient) you can create a custom component around the client as a wrapper.

As for the implementation goes there are many ways If you don’t wanna deal with the complexity of database I would suggest you store your token in session and send it along with subsequent requests.

Thank you @alirz23

i wrote the the User identity Model like this

namespace app\models;

use Yii;
use yii\components;

class User extends Base implements \yii\web\IdentityInterface {
public $id;
public $username;
public $password;
public $enabled;
public $first;
public $last;
public $phone;
public $email;
public $token;
public $tokenexpires;
public $refreshToken;

public function rules() {
return [
[[‘username’,‘password’], ‘required’, ‘on’ => ‘login’],

public function login() {

    $loginObject = Yii::$app->login->AdminSignin(json_encode(['email' => $this->username, 'password' => md5(sha1($this->password))]));
    if ($loginObject->status == 'success') {

		$loginInfo = $loginObject->decodedData;
		$loginUser = new User();
		$loginUser->attributes = $loginInfo;
		if(Yii::$app->user->login($loginUser, 0)){
			return true;

    return false;

public static function findIdentityByAccessToken($token, $type = null) {
    if (\Yii::$app->session->has('userModel') ) {
        $adminUser = \Yii::$app->session->get('userModel');
    }else {
        return NULL;

public static function findByUsername($userObj) {

public function validateAuthKey($authKey) {

public function getAuthKey() {

public function getId() {


and i am using CacheSession like this:

'session' => [
        'class' => 'yii\web\CacheSession',

My current issue is user logs out abruptly when there are multiple ajax calls is there in the page. This is not a constant behavior though.

What could be the reason?