pmtpenza

Untitled

Sep 3rd, 2019
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. <?php
  2. namespace api\controllers;
  3.  
  4. use Yii;
  5. use yii\rest\Controller;
  6. use api\models\LoginForm;
  7. use yii\filters\VerbFilter;
  8. use yii\filters\AccessControl;
  9. use yii\filters\ContentNegotiator;
  10. use yii\web\Response;
  11. use yii\filters\Cors;
  12. use yii\helpers\Json;
  13. use common\models\Token;
  14.  
  15.  
  16.  
  17. class SiteController extends Controller
  18. {
  19. public $enableCsrfValidation = false;
  20. public function actionIndex()
  21. {
  22. return 'api';
  23. }
  24. public function behaviors()
  25. {
  26.  
  27. $behaviors = parent::behaviors();
  28. $behaviors['corsFilter'] = [
  29. 'class' => \yii\filters\Cors::className(),
  30. 'cors' => [
  31. 'Origin' => ['*'],
  32. 'Access-Control-Request-Method' => ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS'],
  33. 'Access-Control-Allow-Credentials' => false,
  34. ],
  35.  
  36. ];
  37. return $behaviors;
  38. }
  39. public function actionLogin()
  40. {
  41. $params = Json::decode(Yii::$app->getRequest()->getRawBody(), true);
  42. $model = new LoginForm(params);
  43. // $model->attributes=$params;
  44. if ($token = $model->auth()) {
  45. return [
  46. 'token' => $token->token,
  47. ];
  48. } else {
  49. return $model;
  50. }
  51. }
  52. public function actionCreate()
  53. {
  54. $params = Json::decode(Yii::$app->getRequest()->getRawBody());
  55. $model = new UserForm();
  56. $model->attributes=$params;
  57. if ($token = $model->auth()) {
  58. return [
  59. 'token' => $token->token,
  60. ];
  61. } else {
  62. return $model;
  63. }
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment