Home > Software design >  Why is my Symfony PdoSessionHandler not working?
Why is my Symfony PdoSessionHandler not working?

Time:04-01

I'm trying to use the PdoSessionHandler in Symfony 5.4. When I'm following the instructions on the Symfony site nothing happens in the database each time a Symfony Session is called. Even when I'm removing the existing session files, removing the Symfony cache and restarting WAMPserver just to be sure, there are still new session files created. Nothing appears in the Symfony logging pointing to this issue. Of course I replaced DATABASE_URL in .env to the values that will work with my database.

My services.yaml looks like this:

# This file is the entry point to configure your own services.
# Files in the packages/ subdirectory configure your dependencies.

# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices.html#use-parameters-for-application-configuration
parameters:

services:
    # default configuration for services in *this* file
    _defaults:
        autowire: true      # Automatically injects dependencies in your services.
        autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.

    # makes classes in src/ available to be used as services
    # this creates a service per class whose id is the fully-qualified class name
    App\:
        resource: '../src/'
        exclude:
            - '../src/DependencyInjection/'
            - '../src/Entity/'
            - '../src/Kernel.php'

    # add more service definitions when explicit configuration is needed
    # please note that last definitions always *replace* previous ones

    Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler:
        arguments:
            - '%env(DATABASE_URL)%'

And my framework.yaml looks like this:

# see https://symfony.com/doc/current/reference/configuration/framework.html
framework:
    secret: '%env(APP_SECRET)%'
    #csrf_protection: true
    http_method_override: false

    # Enables session support. Note that the session will ONLY be started if you read or write from it.
    # Remove or comment this section to explicitly disable session support.
    session:
        handler_id: Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler
        cookie_secure: auto
        cookie_samesite: lax
        storage_factory_id: session.storage.factory.native


    #esi: true
    #fragments: true
    php_errors:
        log: true

when@test:
    framework:
        test: true
        session:
            storage_factory_id: session.storage.factory.mock_file

I created the table with the following query:

    CREATE TABLE `sessions` (
    `sess_id` VARBINARY(128) NOT NULL PRIMARY KEY,
    `sess_data` BLOB NOT NULL,
    `sess_lifetime` INTEGER UNSIGNED NOT NULL,
    `sess_time` INTEGER UNSIGNED NOT NULL,
    INDEX `sessions_sess_lifetime_idx` (`sess_lifetime`)
) COLLATE utf8mb4_bin, ENGINE = InnoDB;

This part will be executed when there is someone logging on:

$session = new Session();
                
if(!isset($_SESSION)){
    $session->start();
}

$session->set('ownerid', $databasestuff);

CodePudding user response:

I see that you are using Session, possibly from the Symfony\Component\HttpFoundation\Session\Session bundle.

I don't think this will work. You might want to use the autowiring for your controller to wireup the SessionInterface.

For example:

namespace App\Controller;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
.
class DummyController extends AbstractController
{
    protected $Session;

    public function __construct(SessionInterface $Session)
    {
         $this->Session = $Session; //Setting the Session interface to the Session var
         if(!$this->Session->isStarted()) //Starting the session if not done yet
             $this->Session->start();
     }
}
  • Related