我正在嘗試在 Symfony 5.4 中使用 PdoSessionHandler。當我按照Symfony 站點上的說明進行操作時,每次呼叫 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
我使用以下查詢創建了表:
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);
uj5u.com熱心網友回復:
我看到您正在使用 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();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/456403.html
上一篇:Symfony中的動態資料庫連接
