2016-09-23 60 views
0

我想給Yii2用戶數據的第三方PHP應用程序訪問(HumHub),並嘗試這樣的:如何第三方應用程序訪問Yii2(HumHub)

function getUserId() { 
     require_once('../protected/vendor/yiisoft/yii2/Yii.php'); 
     $yiiConfig = require('../protected/config/common.php'); 
     (new humhub\components\Application($yiiConfig)); 
     $user = Yii::$app->user->identity; 

     return $user; 
    } 

這是行不通的。直到new humhub\components\Application($yiiConfig)之前沒有任何錯誤,但第三方應用程序中斷並沒有拋出任何錯誤,並且該函數不返回任何內容。

我確實發現this solution哪些不起作用。

是否有理由這不起作用或有沒有替代解決方案來正確獲取Yii2用戶數據?

+0

你讀[**這個官方**](http://www.yiiframework.com/doc-2.0/guide-tutorial-yii-integration.html#使用-yii-in-others)文件? – SaidbakR

回答

0

這是如何做到這一點的HumHub V1.0現在

require_once('../protected/vendor/yiisoft/yii2/Yii.php'); 
    $config = yii\helpers\ArrayHelper::merge(
     require('../protected/humhub/config/common.php'), 
     require('../protected/humhub/config/web.php'), 
     (is_readable('../protected/config/dynamic.php')) ? require('../protected/config/dynamic.php') : [], 
     require('../protected/config/common.php'), 
     require('../protected/config/web.php') 
    ); 
    new yii\web\Application($config); // No 'run()' invocation! 

我可以得到$用戶對象:

$user = Yii::$app->user->identity; 
+0

這不是一個答案,只需編輯你的問題並添加它。 – SaidbakR

+1

爲什麼這不是一個答案?當我在問題中運行代碼時,它不起作用。當我在這個答案中運行代碼時,它工作。 – lilbiscuit

+0

對不起,我錯誤地認爲'HumHub V1.0'是'Yii V1.0' – SaidbakR

0

事實上的錯誤應該被拋出,但是,錯誤的設置您的PHP可能會被覆蓋或設置爲不顯示錯誤。

您可以調用未定義的對象Yii::$app->user->identity。原因是,從documentation因爲你還沒有初始化Yii對象。所以,你的代碼應該如下:

function getUserId() { 
     require_once('../protected/vendor/yiisoft/yii2/Yii.php'); 
     $yiiConfig = require('../protected/config/common.php'); 
     (new humhub\components\Application($yiiConfig)); // try to comment this line too if it does not work 
     // Add The Following Line 
     new yii\web\Application($yiiConfig); // Do NOT call run() here 
     $user = Yii::$app->user->identity; 

     return $user; 
    } 
相關問題