skip to Main Content

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' => 'consolecontrollers',
    'controllerMap' => [
        'fixture' => [
            'class' => 'yiiconsolecontrollersFixtureController',
            'namespace' => 'commonfixtures',
          ],
    ],
    'components' => [
        'log' => [
            'targets' => [
                [
                    'class' => 'yiilogFileTarget',
                    'levels' => ['error', 'warning', ],
                ],
            ],
        ],

    
        'session' => [ // for use session in console application
            'class' => 'yiiwebSession',

        ],
      

but I get this error

PHP Warning 'yiibaseErrorException' with message 'session_set_cookie_params(): Session cookie parameters cannot be changed after headers have already been sent'

in C:xamppwwwdev_toolvendoryiisoftyii2webSession.php:432

Stack trace:
#0 [internal function]: yiibaseErrorHandler->handleError(2, 'session_set_coo...', 'C:\xampp\www\...', 432)
#1 C:xamppwwwdev_toolvendoryiisoftyii2webSession.php(432): session_set_cookie_params(Array)
#2 C:xamppwwwdev_toolvendoryiisoftyii2webSession.php(150): yiiwebSession->setCookieParamsInternal()
#3 C:xamppwwwdev_toolvendoryiisoftyii2webSession.php(712): yiiwebSession->open()
#4 C:xamppwwwdev_toolconsolecontrollersToolboxController.php(1256): yiiwebSession->set('conpanyId', 156)
#5 [internal function]: consolecontrollersToolboxController->actionCalculateQuestionnaireStatusFromQuestionnaire()
#6 C:xamppwwwdev_toolvendoryiisoftyii2baseInlineAction.php(57): call_user_func_array(Array, Array)
#7 C:xamppwwwdev_toolvendoryiisoftyii2baseController.php(181): yiibaseInlineAction->runWithParams(Array)
#8 C:xamppwwwdev_toolvendoryiisoftyii2consoleController.php(184): yiibaseController->runAction('calculate-quest...', Array)
#9 C:xamppwwwdev_toolvendoryiisoftyii2baseModule.php(534): yiiconsoleController->runAction('calculate-quest...', Array)
#10 C:xamppwwwdev_toolvendoryiisoftyii2consoleApplication.php(181): yiibaseModule->runAction('toolbox/calcula...', Array)
#11 C:xamppwwwdev_toolvendoryiisoftyii2consoleApplication.php(148): yiiconsoleApplication->runAction('toolbox/calcula...', Array)
#12 C:xamppwwwdev_toolvendoryiisoftyii2baseApplication.php(392): yiiconsoleApplication->handleRequest(Object(yiiconsoleRequest))
#13 C:xamppwwwdev_toolyii(27): yiibaseApplication->run()
#14 {main}

I tried to close and open the session. But nothing happen… Can please someone help me?

Thank you!

2

Answers


  1. 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 yiiwebApplication) {
        // work with console
    }
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search