I am trying to run a console controller from the terminal, but i am getting this errors every time
我试图从终端运行控制台控制器,但我每次都会收到此错误
Error: Getting unknown property: yii\console\Application::user
here is the controller
这是控制器
class TestController extends \yii\console\Controller {
public function actionIndex() {
echo 'this is console action';
} }
and this is the concole config
这是concole配置
return [
'id' => 'app-console',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'controllerNamespace' => 'console\controllers',
'modules' => [],
'components' => [
'log' => [
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
],
'params' => $params];
I tried running it using these commands with no luck
我尝试使用这些命令运行它没有运气
php yii test/index
php yii test
php ./yii test
can anyone help please?
有人可以帮忙吗?
3 个解决方案
#1
10
Console application does not have Yii->$app->user
. So, you need to configure user
component in config\console.php
.
控制台应用程序没有Yii - > $ app-> user。因此,您需要在config \ console.php中配置用户组件。
like as,
像,
config\console.php
配置\ console.php
'components' => [
.........
......
'user' => [
'class' => 'yii\web\User',
'identityClass' => 'app\models\User',
//'enableAutoLogin' => true,
],
'session' => [ // for use session in console application
'class' => 'yii\web\Session'
],
.......
]
More info about your problem see this : Link
有关您的问题的更多信息,请参阅:链接
OR
要么
Visit following link : Yii2 isGuest giving exception in console application
请访问以下链接:Yii2 isGuest在控制台应用程序中提供异常
Note : There's no session in console application.
注意:控制台应用程序中没有会话。
#2
4
Set in \console\config\main.php
在\ console \ config \ main.php中设置
return [
'id' => 'app-console',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'controllerNamespace' => 'console\controllers',
'components' => [
'log' => [
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'user' => [
'class' => 'yii\web\User',
'identityClass' => 'app\models\Credential',// class that implements IdentityInterface
//'enableAutoLogin' => true,
],
],
'params' => $params,
];
now in your \console\controller\AbcController.php add init method
现在在你的\ console \ controller \ AbcController.php中添加init方法
public function init() {
parent::init();
Yii::$app->user->setIdentity(Credential::findOne(['id'=><cronloginid>]));
}
create a cron login and pass that login id in variable with this config your Blameable Behavior of yii2 will work
创建一个cron登录并使用此配置在变量中传递该登录ID,yii2的Blameable Behavior将起作用
#3
1
As @GAMITG said, you must config user component in config file, but unfortunately, you couldn't access session in console, that's because session is not available in console. Maybe you could solve the problem like this:
正如@GAMITG所说,你必须在配置文件中配置用户组件,但遗憾的是,你无法在控制台中访问会话,因为会话在控制台中不可用。也许你可以像这样解决问题:
$user_id = isset(Yii::$app->user->id) ? Yii::$app->user->id : 0;
#1
10
Console application does not have Yii->$app->user
. So, you need to configure user
component in config\console.php
.
控制台应用程序没有Yii - > $ app-> user。因此,您需要在config \ console.php中配置用户组件。
like as,
像,
config\console.php
配置\ console.php
'components' => [
.........
......
'user' => [
'class' => 'yii\web\User',
'identityClass' => 'app\models\User',
//'enableAutoLogin' => true,
],
'session' => [ // for use session in console application
'class' => 'yii\web\Session'
],
.......
]
More info about your problem see this : Link
有关您的问题的更多信息,请参阅:链接
OR
要么
Visit following link : Yii2 isGuest giving exception in console application
请访问以下链接:Yii2 isGuest在控制台应用程序中提供异常
Note : There's no session in console application.
注意:控制台应用程序中没有会话。
#2
4
Set in \console\config\main.php
在\ console \ config \ main.php中设置
return [
'id' => 'app-console',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'controllerNamespace' => 'console\controllers',
'components' => [
'log' => [
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'user' => [
'class' => 'yii\web\User',
'identityClass' => 'app\models\Credential',// class that implements IdentityInterface
//'enableAutoLogin' => true,
],
],
'params' => $params,
];
now in your \console\controller\AbcController.php add init method
现在在你的\ console \ controller \ AbcController.php中添加init方法
public function init() {
parent::init();
Yii::$app->user->setIdentity(Credential::findOne(['id'=><cronloginid>]));
}
create a cron login and pass that login id in variable with this config your Blameable Behavior of yii2 will work
创建一个cron登录并使用此配置在变量中传递该登录ID,yii2的Blameable Behavior将起作用
#3
1
As @GAMITG said, you must config user component in config file, but unfortunately, you couldn't access session in console, that's because session is not available in console. Maybe you could solve the problem like this:
正如@GAMITG所说,你必须在配置文件中配置用户组件,但遗憾的是,你无法在控制台中访问会话,因为会话在控制台中不可用。也许你可以像这样解决问题:
$user_id = isset(Yii::$app->user->id) ? Yii::$app->user->id : 0;