2015-11-03 76 views
1

我yii2新的,我想在登錄用戶使用REST API,但不能幹這事已經設置基本的REST API從這個博客:如何登錄使用REST API用戶yii2

budiirawan.com/設置的RESTful-API-yii2/

之後我已經創建:

API \模塊\ V1 \控制器\ SiteController.php

<?php 
namespace api\modules\v1\controllers; 

use Yii; 
use yii\filters\AccessControl; 
use yii\web\Controller; 
use common\models\LoginForm; 
use yii\filters\VerbFilter; 
use yii\rest\ActiveController; 
/** 
* Site controller 
*/ 
class SiteController extends ActiveController 
{ 
    /** 
    * @inheritdoc 
    */ 
    public $modelClass = 'api\modules\v1\models\user';  


    public function actionIndex() 
    { 
      if (!\Yii::$app->user->isGuest) { 
      return $this->goHome(); 
     } 

     $model = new LoginForm(); 
     if ($model->load(Yii::$app->request->post()) && $model->login()) { 
      return $this->goBack(); 
     } else { 
      return $this->render('login', [ 
       'model' => $model, 
      ]); 
     } 
    } 

    public function actionLogout() 
    { 
     Yii::$app->user->logout(); 

     return $this->goHome(); 
    } 
} 

而創建的模型

RtWorkForce \ API \模塊\ V1 \型號\ user.php的

<?php 
namespace api\modules\v1\models; 
use \yii\db\ActiveRecord; 

/** 
* User Model 
* 
*/ 
class User extends ActiveRecord 
{ 
    /** 
    * @inheritdoc 
    */ 
    public static function tableName() 
    { 
     return '{{%user}}'; 

    } 


} 

這裏是我的main.php

<?php 

$params = array_merge(
    require(__DIR__ . '/../../common/config/params.php'), 
    require(__DIR__ . '/../../common/config/params-local.php'), 
    require(__DIR__ . '/params.php'), 
    require(__DIR__ . '/params-local.php') 
); 

return [ 
    'id' => 'app-api', 
    'basePath' => dirname(__DIR__),  
    'bootstrap' => ['log'], 
    'modules' => [ 
     'v1' => [ 
      'basePath' => '@app/modules/v1', 
      'class' => 'api\modules\v1\Module' 
     ] 
    ], 
    'components' => [ 
       'request' => [ 
        'parsers' => [ 
         'application/json' => 'yii\web\JsonParser', 
        ] 
       ], 
     'user' => [ 
      'identityClass' => 'common\models\User', 
      'enableAutoLogin' => false, 
     ], 
     'log' => [ 
      'traceLevel' => YII_DEBUG ? 3 : 0, 
      'targets' => [ 
       [ 
        'class' => 'yii\log\FileTarget', 
        'levels' => ['error', 'warning'], 
       ], 
      ], 
     ], 
     'urlManager' => [ 
      'enablePrettyUrl' => true, 
      'enableStrictParsing' => true, 
      'showScriptName' => false, 
      'rules' => [ 
       [ 
        'class' => 'yii\rest\UrlRule', 
        'controller' => ['v1/country','v1/user','v1/site'], 
        'tokens' => [ 
         '{id}' => '<id:\\w+>' 
        ] 

       ] 
      ],   
     ] 
    ], 
    'params' => $params, 
]; 

但IT不工作,我不知道我錯在哪裏?

回答

4

Yii 2.0 REST Authentication docs

與Web應用程序,RESTful API中通常是無狀態的,這意味着 會話或不應該使用的cookie。

而且從this other docs有關用戶類實現警予\網絡\ IdentityInterface

如果你的應用是一個純粹的無狀態的RESTful應用程序,你會 只需要實現findIdentityByAccessToken( )和getId(),而 將所有其他方法留空。


問題的REST約爲路由。如果遵循基於令牌的認證,那麼如果發送給服務器的請求持有有效的令牌,則它應返回資源收集。否則它應該被拒絕。在這種情況下

登錄過程,是一個請求拿着用戶名/密碼對將與一個有效的令牌由服務器進行交換,即令牌你要與你的下一個所有的請求,包括。

如果會議是爲Yii的文檔(見上鏈接)中描述的設置enableSessionfalse在服務器端禁用和推薦的無國籍自然的休息,然後\Yii::$app->user->isGuest不應該提供的信息爲你服務器將不會有任何會話來獲取它。 (除非驗證令牌的有效性,而不是檢查會話

當建立一個類擴展yii\rest\ActiveController你無法呈現一個HTML頁面,如:

return $this->render('login', [ 
    'model' => $model, 
]); 

或重定向到一個不同的HTML頁面,如:

return $this->goHome(); 

在構建基於HTML的網絡應用程序而不是yii\rest\ActiveController時,這將與yii\base\Controller一起使用。使用ActiveController,您只需返回將在輸出之前序列化爲jsonxml的數據。

詳情請參閱Yii RESTful API framework documentations。那麼你會發現在這個偉大的教程有用的信息如何實現Yii2 Rest認證

http://blog.neattutorials.com/angularjs-and-yii2-part-2-authentication/

+0

我試圖給出解決方案,一旦完成將會讓你知道。 :) – Arunendra