Refactoring ZF2 to Lamians Module.php onBootstrap

Hello everyone :slight_smile:

i migrated a project from zf2 to laminas, everything went kinda smooth, but its an pretty old project. Now i want to make some elements in a better practice way and also include PHPUnit-Tests.

But my first test directly gets “smashed” because of some settings in my Module.php, to be more clear in the onBootstrap function.

  1. ApplicationTest\Controller\IndexControllerTest::testIndexActionCanBeAccessed
    ini_set(): Headers already sent. You cannot change the session module’s ini settings at this time

/var/www/vendor/laminas/laminas-session/src/Config/SessionConfig.php:147
/var/www/vendor/laminas/laminas-session/src/Config/StandardConfig.php:146
/var/www/vendor/laminas/laminas-session/src/Config/SessionConfig.php:105
/var/www/vendor/laminas/laminas-session/src/Config/StandardConfig.php:115
/var/www/module/Application/Module.php:441
/var/www/vendor/laminas/laminas-eventmanager/src/EventManager.php:321
/var/www/vendor/laminas/laminas-eventmanager/src/EventManager.php:178
/var/www/vendor/laminas/laminas-mvc/src/Application.php:310
/var/www/vendor/laminas/laminas-test/src/PHPUnit/Controller/AbstractControllerTestCase.php:319
/var/www/module/Application/test/Controller/IndexControllerTest.php:31

My Module.php looks like this:

$sessionConfig = new SessionConfig();
$options[‘use_only_cookies’] = ‘off’;
$options[‘use_trans_sid’] = ‘on’;
$options[‘use_cookies’] = ‘on’;
$options[‘name’] = ‘PHPSESSID’;
$options[‘cookie_httponly’] = false;
$sessionConfig->setOptions($options);

Is this the wrong “position” to setup some session config stuff? Where would this code belong to?
I got quite a lot code in the onBootstrap Part. Is there any rescource, how i could face that issue and make this legacy Project better?

Greetings from Germany
mhaendler

When you run the same code in the browser environment, does it blow up? I have had similar problems and never completely understood what was going on, but what worked was wrapping the session initialization stuff in a conditional
if ('testing' != getenv('environment')) { /* do all that */}

I fixed it by checking PHP_SAPI === ‘cli’ dont set Session Options, but! my next problem is, i dont get a working session within PHPUnit, some of my api calls require a session_id but i dont get have / get one

ah, I see. this isn’t a direct answer, but could you set those session settings some other way? e.g., via ini_set or a configuration file?

the resource I used as a guide was https://olegkrivtsov.github.io/using-zend-framework-3-book/html/en/Working_with_Sessions.html which is now a little dated, referring to Zend rather than Laminas, but it may still be of value.