为什么我的 Symfony PdoSessionHandler 不工作?
Why is my Symfony PdoSessionHandler not working?
我正在尝试使用 Symfony 5.4 中的 PdoSessionHandler。当我关注 the instructions on the Symfony site 时,每次调用 Symfony 会话时数据库中都没有任何反应。即使我正在删除现有的会话文件,删除 Symfony 缓存并重新启动 WAMPserver 只是为了确定,仍然会创建新的会话文件。
Symfony 日志记录中没有任何内容指向此问题。
当然,我将 .env 中的 DATABASE_URL 替换为适用于我的数据库的值。
我的 services.yaml 看起来像这样:
# 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)%'
我的 framework.yaml 看起来像这样:
# 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
我使用以下查询创建了 table:
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;
这部分会在有人登录时执行:
$session = new Session();
if(!isset($_SESSION)){
$session->start();
}
$session->set('ownerid', $databasestuff);
我看到您正在使用 Session,可能来自 Symfony\Component\HttpFoundation\Session\Session 包。
我认为这行不通。
您可能希望使用控制器的自动装配来连接 SessionInterface。
例如:
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();
}
}
我正在尝试使用 Symfony 5.4 中的 PdoSessionHandler。当我关注 the instructions on the Symfony site 时,每次调用 Symfony 会话时数据库中都没有任何反应。即使我正在删除现有的会话文件,删除 Symfony 缓存并重新启动 WAMPserver 只是为了确定,仍然会创建新的会话文件。 Symfony 日志记录中没有任何内容指向此问题。 当然,我将 .env 中的 DATABASE_URL 替换为适用于我的数据库的值。
我的 services.yaml 看起来像这样:
# 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)%'
我的 framework.yaml 看起来像这样:
# 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
我使用以下查询创建了 table:
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;
这部分会在有人登录时执行:
$session = new Session();
if(!isset($_SESSION)){
$session->start();
}
$session->set('ownerid', $databasestuff);
我看到您正在使用 Session,可能来自 Symfony\Component\HttpFoundation\Session\Session 包。
我认为这行不通。 您可能希望使用控制器的自动装配来连接 SessionInterface。
例如:
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();
}
}