Bridge a legacy Application with Symfony Sessions

Bridge a legacy Application with Symfony Sessions

If you’re integrating the Symfony full-stack Framework into a legacy application that starts the session with session_start(), you may still be able to use Symfony’s session management by using the PHP Bridge session.

If the application has its own PHP save handler, you can specify null for the handler_id:

  • YAML

    1. # config/packages/framework.yaml
    2. framework:
    3. session:
    4. storage_id: session.storage.php_bridge
    5. handler_id: ~
  • XML

    1. <!-- config/packages/framework.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xmlns:framework="http://symfony.com/schema/dic/symfony"
    6. xsi:schemaLocation="http://symfony.com/schema/dic/services
    7. https://symfony.com/schema/dic/services/services-1.0.xsd">
    8. <framework:config>
    9. <framework:session storage-id="session.storage.php_bridge"
    10. handler-id="null"
    11. />
    12. </framework:config>
    13. </container>
  • PHP

    1. // config/packages/framework.php
    2. $container->loadFromExtension('framework', [
    3. 'session' => [
    4. 'storage_id' => 'session.storage.php_bridge',
    5. 'handler_id' => null,
    6. ],
    7. ]);

Otherwise, if the problem is that you cannot avoid the application starting the session with session_start(), you can still make use of a Symfony based session save handler by specifying the save handler as in the example below:

  • YAML

    1. # config/packages/framework.yaml
    2. framework:
    3. session:
    4. storage_id: session.storage.php_bridge
    5. handler_id: session.handler.native_file
  • XML

    1. <!-- config/packages/framework.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xmlns:framework="http://symfony.com/schema/dic/symfony"
    6. xsi:schemaLocation="http://symfony.com/schema/dic/services
    7. https://symfony.com/schema/dic/services/services-1.0.xsd">
    8. <framework:config>
    9. <framework:session storage-id="session.storage.php_bridge"
    10. handler-id="session.storage.native_file"
    11. />
    12. </framework:config>
    13. </container>
  • PHP

    1. // config/packages/framework.php
    2. $container->loadFromExtension('framework', [
    3. 'session' => [
    4. 'storage_id' => 'session.storage.php_bridge',
    5. 'handler_id' => 'session.storage.native_file',
    6. ],
    7. ]);

Note

If the legacy application requires its own session save handler, do not override this. Instead set handler_id: ~. Note that a save handler cannot be changed once the session has been started. If the application starts the session before Symfony is initialized, the save handler will have already been set. In this case, you will need handler_id: ~. Only override the save handler if you are sure the legacy application can use the Symfony save handler without side effects and that the session has not been started before Symfony is initialized.

For more details, see Integrating with Legacy Sessions.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.