I want to use the session in the console for use a function from model. I use in the config from console the session like that:
main.php
main.php
...
return [
'id' => 'app-console',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'controllerNamespace' => 'console\controllers',
'controllerMap' => [
'fixture' => [
'class' => 'yii\console\controllers\FixtureController',
'namespace' => 'common\fixtures',
],
],
'components' => [
'log' => [
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning', ],
],
],
],
'session' => [ // for use session in console application
'class' => 'yii\web\Session',
],
but I get this error
PHP Warning 'yii\base\ErrorException' with message 'session_set_cookie_params(): Session cookie parameters cannot be changed after headers have already been sent'
in C:\xampp\www\dev_tool\vendor\yiisoft\yii2\web\Session.php:432
Stack trace:
#0 [internal function]: yii\base\ErrorHandler->handleError(2, 'session_set_coo...', 'C:\\xampp\\www\\...', 432)
#1 C:\xampp\www\dev_tool\vendor\yiisoft\yii2\web\Session.php(432): session_set_cookie_params(Array)
#2 C:\xampp\www\dev_tool\vendor\yiisoft\yii2\web\Session.php(150): yii\web\Session->setCookieParamsInternal()
#3 C:\xampp\www\dev_tool\vendor\yiisoft\yii2\web\Session.php(712): yii\web\Session->open()
#4 C:\xampp\www\dev_tool\console\controllers\ToolboxController.php(1256): yii\web\Session->set('conpanyId', 156)
#5 [internal function]: console\controllers\ToolboxController->actionCalculateQuestionnaireStatusFromQuestionnaire()
#6 C:\xampp\www\dev_tool\vendor\yiisoft\yii2\base\InlineAction.php(57): call_user_func_array(Array, Array)
#7 C:\xampp\www\dev_tool\vendor\yiisoft\yii2\base\Controller.php(181): yii\base\InlineAction->runWithParams(Array)
#8 C:\xampp\www\dev_tool\vendor\yiisoft\yii2\console\Controller.php(184): yii\base\Controller->runAction('calculate-quest...', Array)
#9 C:\xampp\www\dev_tool\vendor\yiisoft\yii2\base\Module.php(534): yii\console\Controller->runAction('calculate-quest...', Array)
#10 C:\xampp\www\dev_tool\vendor\yiisoft\yii2\console\Application.php(181): yii\base\Module->runAction('toolbox/calcula...', Array)
#11 C:\xampp\www\dev_tool\vendor\yiisoft\yii2\console\Application.php(148): yii\console\Application->runAction('toolbox/calcula...', Array)
#12 C:\xampp\www\dev_tool\vendor\yiisoft\yii2\base\Application.php(392): yii\console\Application->handleRequest(Object(yii\console\Request))
#13 C:\xampp\www\dev_tool\yii(27): yii\base\Application->run()
#14 {main}
I tried to close and open the session. But nothing happen... Can please someone help me?
Thank you!
CodePudding user response:
You can not and should not use it in console. Instead, modify your model to detect console application and avoid working with session in this case:
if (Yii::$app instanceof \yii\web\Application) {
// work with console
}
CodePudding user response:
Using session in console is bad idea, but your error is about else "Session cookie parameters cannot be changed after headers have already been sent" - it's mean than you out some information before use session. Try to remove all echo/var_dump in your code.