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


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;

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

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 which is now a little dated, referring to Zend rather than Laminas, but it may still be of value.