Подскажите как получить токен первый раз?!
1. так я сделал API как модуль
2. я могу получать данные через API, но приходиться передавать ?access-token=<custom_string_in_user_tbl>
3. access_token это любая случайная строка или или обязательно Base64(username:password)
4. хотел бы для своего сайта сделать АПИ что бы мобильное приложение подключить
идея пришла сделать дополнительный action (actionLogin) передавать туда email и пароль из приложения и получить в ответ токен
и потом уже этим токенов получать данные.
однако не могу добавить кастовую action
правила для урла
Код: Выделить всё
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
['class' => 'yii\rest\UrlRule',
'controller' => //http://yii2.kz/api/user/index?access-token=fsafsdfs
['user', 'news'
/*'PUT,PATCH users/<id>' => 'api/user/update',
'DELETE users/<id>' => 'api/user/delete',
'GET,HEAD users/<id>' => 'api/user/view',
'POST users' => 'api/user/create',
'GET,HEAD users' => 'api/user/index',
'users/<id>' => 'api/user/options',
'users' => 'api/user/options', */
],
'extraPatterns' => [
'GET custom' => 'custom',
],
],
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
]
],
Код: Выделить всё
<?php
namespace app\controllers;
namespace app\modules\api\controllers;
use yii\web\Controller;
use yii\rest\ActiveController;
use yii\web\Response;
use yii\filters\auth\CompositeAuth;
use yii\filters\auth\HttpBasicAuth;
use yii\filters\auth\HttpBearerAuth;
use yii\filters\auth\QueryParamAuth;
class UserController extends ActiveController
{
public $modelClass = 'app\models\User';
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors['authenticator'] = [
'except' => 'customAction',
'class' => CompositeAuth::className(),
'authMethods' => [
HttpBasicAuth::className(),
HttpBearerAuth::className(),
QueryParamAuth::className(),
],
//
];
$behaviors['verbs'] = [
'class' => \yii\filters\VerbFilter::className(),
'actions' => [
'actionCustom' => ['get', 'head'],
],
];
return $behaviors;
}
public $serializer = [
'class' => 'yii\rest\Serializer',
'collectionEnvelope' => 'items',
];
public function actionCustom()
{
$model = $this->findModel(Yii::$app->user->id);
echo $model;
/*
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('update', [
'model' => $model
]);
}
*/
}
}
и как к нему обращаться api/user/custom