PHP code example of hoaaah / yii2-rest-api-template

1. Go to this page and download the library: Download hoaaah/yii2-rest-api-template library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hoaaah / yii2-rest-api-template example snippets



return [
    'components' => [
        'db' => [
            'class' => 'yii\db\Connection',
            'dsn' => 'mysql:host=localhost;dbname=your_db_name',
            'username' => 'root',
            'password' => '',
            'charset' => 'utf8',
        ],
    ],
];


return [
    'useHttpBasicAuth' => true,
    'useHttpBearerAuth' => true,
    'useQueryParamAuth' => true,
    'useRateLimiter' => false,
];

use app\helpers\BehaviorsFromParamsHelper;
use yii\rest\ActiveController;

class PostController extends ActiveController
{
    public $modelClass = 'app\models\Post';

    public function behaviors()
    {
        $behaviors = parent::behaviors();
        $behaviors = BehaviorsFromParamsHelper::behaviors($behaviors);
        // if you need other behaviors method use like this
        // $behaviors['otherMethods'] = $value;
        return $behaviors;
    }
}
 
public $tokenExpiration = 60 * 24 * 365; // in seconds

$accessToken = AccessToken::findOne(['token' => $token]);
$accessToken->expireThisToken();
 
$user = Yii::$app->user->identity; // or User::findOne($id)
AccessToken::makeAllUserTokenExpiredByUserId($user->id);