Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace api\controllers;
- use Yii;
- use yii\rest\Controller;
- use api\models\LoginForm;
- use yii\filters\VerbFilter;
- use yii\filters\AccessControl;
- use yii\filters\ContentNegotiator;
- use yii\web\Response;
- use yii\filters\Cors;
- use yii\helpers\Json;
- use common\models\Token;
- class SiteController extends Controller
- {
- public $enableCsrfValidation = false;
- public function actionIndex()
- {
- return 'api';
- }
- public function behaviors()
- {
- $behaviors = parent::behaviors();
- $behaviors['corsFilter'] = [
- 'class' => \yii\filters\Cors::className(),
- 'cors' => [
- 'Origin' => ['*'],
- 'Access-Control-Request-Method' => ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS'],
- 'Access-Control-Allow-Credentials' => false,
- ],
- ];
- return $behaviors;
- }
- public function actionLogin()
- {
- $params = Json::decode(Yii::$app->getRequest()->getRawBody(), true);
- $model = new LoginForm(params);
- // $model->attributes=$params;
- if ($token = $model->auth()) {
- return [
- 'token' => $token->token,
- ];
- } else {
- return $model;
- }
- }
- public function actionCreate()
- {
- $params = Json::decode(Yii::$app->getRequest()->getRawBody());
- $model = new UserForm();
- $model->attributes=$params;
- if ($token = $model->auth()) {
- return [
- 'token' => $token->token,
- ];
- } else {
- return $model;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment