我想让第三方PHP应用程序访问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)之前没有错误,但是第三方应用程序会中断,没有抛出错误,函数也不会返回任何内容。
我确实找到了不起作用的这个解决方案。
这是否有不起作用的原因,或者是否有一种适当获取Yii2用户数据的替代解决方案?
发布于 2016-09-24 12:02:43
这是如何在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对象:
$user = Yii::$app->user->identity;发布于 2016-09-24 12:38:30
的确,应该抛出错误,但是,PHP的错误设置可能会被覆盖或设置为不显示错误。
您可以调用未定义的对象Yii::$app->user->identity。原因来自文档,因为您还没有初始化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;
}https://stackoverflow.com/questions/39670108
复制相似问题