I have updated symfony to 5.3 and get deprecations logs like
User Deprecated: Since symfony/framework-bundle 5.3: The
session.storage.factory.service
service is deprecated, usesession.storage.factory.native
,session.storage.factory.php_bridge
orsession.storage.factory.mock_file
instead.
I think it is caused by using TokenStrageInterface::getToken()
but I cannot find the solution to solve it.
The code I use is like this.
<?php
namespace App\EventSubscriber;
use App\Entity\User;
use Gedmo\Loggable\LoggableListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface;
class DoctrineExtensionSubscriber implements EventSubscriberInterface
{
/**
* @var LoggableListener
*/
private LoggableListener $loggableListener;
/**
* @var TokenStorageInterface
*/
private TokenStorageInterface $tokenStorage;
public function __construct(LoggableListener $loggableListener,
TokenStorageInterface $tokenStorage
)
{
$this->loggableListener = $loggableListener;
$this->tokenStorage = $tokenStorage;
}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::CONTROLLER => [
'onKernelController',
-10,
],
];
}
public function onKernelController(ControllerEvent $event): void
{
if (!$event->isMainRequest()) {
return;
}
if ($this->tokenStorage?->getToken()?->isAuthenticated() === true) {
$user = $this->tokenStorage->getToken()->getUser();
$controller = $event->getController();
if (is_array($event->getController())) {
$controller = $event->getController()[0];
}
if ($user instanceof User) {
$this->loggableListener->setUsername($user->getFullName());
return;
}
$this->loggableListener->setUsername('Anonymous');
}
}
}
I've thought to edit config file, but I can't determine where to change. Please see the following code for config/packages/framework.yaml
:
# see https://symfony.com/doc/current/reference/configuration/framework.html
framework:
secret: '%env(APP_SECRET)%'
#csrf_protection: true
#http_method_override: true
# 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: null
cookie_secure: auto
cookie_samesite: lax
save_path: '%kernel.project_dir%/var/sessions/%kernel.environment%'
#esi: true
#fragments: true
php_errors:
log: true
Your framework.yaml session section should look like:
session:
handler_id: null
cookie_secure: auto
cookie_samesite: lax
storage_factory_id: session.storage.factory.native
This is the default config you get with a new 5.3 project. You can leave the save_path entry if you want. The storage_value_id was introduced in 5.3.
According to the storage_factory_id docs, the default value should already be factory.native. Which implies that you don't actually need the entry at all.
However, bin/console debug:config framework session shows different results if you leave it out. Not sure it that is an error or not.
In any event, add the storage_factory_id and the error should go away.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With