Why is my Symfony PdoSessionHandler not working? - php

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);

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

how to fix this error "App\Controller\CoreController" has no container set, did you forget to define it as a service subscriber?

i was deconnected any times at my application so i've want fix the problem but i've this error ans my folders / services was correctly configured. I would like to know if anyone has ever encountered this problem and if so how to fix it.
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'
App\Controller\:
resource: '../src/Controller'
tags: ['controller.service_arguments']
# add more service definitions when explicit configuration is needed
# please note that last definitions always *replace* previous ones
CoreController
namespace App\Controller;
use App\Entity\Intervention;
use Doctrine\ORM\Mapping\Id;
use Doctrine\DBAL\Schema\Index;
use App\Repository\OperateurRepository;
use Doctrine\Persistence\ManagerRegistry;
use App\Repository\InterventionRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class CoreController extends AbstractController
{```
I've fix with
calls:
- [setContainer, ["#service_container"]]
in services.yaml

service error after upgraded from symfony 3.3 to symfony 3.4

I've upgraded my symfony projet version from 3.3 to 3.4 a few month ago and everything woked find but i recently tried to ad an event listener and then i've got that error
The configuration key "public" cannot be used to define a default value in "/home/pc-dev/Labs/Projets-jebuy/je-buy.com/app/config/services.yml". Allowed keys are "private", "tags", "autowire", "autoconfigure", "bind" in /home
/pc-dev/Labs/Projets-jebuy/je-buy.com/app/config/services.yml (which is being imported from "/home/pc-dev/Labs/Projets-jebuy/je-buy.com/app/config/config.yml").
obviuosly nothing seriuos i edited the default service.yml file from this
# default configuration for services in *this* file
_defaults:
# automatically injects dependencies in your services
autowire: true
# automatically registers your services as commands, event subscribers, etc.
autoconfigure: true
# this means you cannot fetch services directly from the container via $container->get()
# if you need to do this, you can override this setting on individual services
public: false
twig.extension:
class: AppBundle\Twig\Extension\MenuExtension
arguments:
- '#doctrine'
tags:
- {name : twig.extension }
# service.extension:
# class: AdminBundle\DependencyInjection\AdminExtension
# arguments: []
# tags: []
to this (because in symfony 3.4 or 4, all services are by default private)
services:
# default configuration for services in *this* file
_defaults:
# automatically injects dependencies in your services
autowire: true
# automatically registers your services as commands, event subscribers, etc.
autoconfigure: true
# this means you cannot fetch services directly from the container via $container->get()
# if you need to do this, you can override this setting on individual services
private: true
twig.extension:
class: AppBundle\Twig\Extension\MenuExtension
arguments:
- '#doctrine'
tags:
- {name : twig.extension }
# service.extension:
# class: AdminBundle\DependencyInjection\AdminExtension
# arguments: []
# tags: []
but when i tried to run the server i've now got this error
The service "templating.loader.cache" has a dependency on a non-existent service "templating.loader.wrapped".
and i don't now how to solve it and where is that template.loder.wrapped service.

How do you use RedisSessionHandler in Symfony 4? Broken docs

The documentation is broken and leads to a 404 page. I'm not too good at reverse engineering classes like this, any tips on how to setup services.yml to use it?
Doc page
https://symfony.com/doc/current/components/http_foundation/session_configuration.html
Session Handler 404
https://api.symfony.com/4.1/Symfony/Component/HttpFoundation/Session/Storage/Handler/RedisSessionHandler.html
Git Page for Session Handler
https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/RedisSessionHandler.php
You need to define 2 services:
One for the Redis connection.
One for RedisSessionHandler that will make use of that connection.
Edit the services file:
# config/services.yaml
services:
Redis:
class: Redis
calls:
- method: connect
arguments:
- '%env(REDIS_HOST)%'
- '%env(int:REDIS_PORT)%'
# If you need key prefix
# - method: setOption
# arguments:
# - !php/const Redis::OPT_PREFIX
# - 'my_prefix'
Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler:
arguments:
- '#Redis'
(note that I stored here the Redis host & port as environment variables, but you can define them elsewhere if needed).
You can now make use of the service as your session handler:
# config/packages/framework.yaml
framework:
session:
handler_id: Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler
I struggled a bit too, and wrote a more detailed articled: http://blog.michaelperrin.fr/2018/08/14/redis-session-handling-in-symfony/
For adding custom prefix, maybe the following is better:
Redis:
class: Redis
calls:
- method: connect
arguments:
- '%env(REDIS_HOST)%'
- '%env(int:REDIS_PORT)%'
# - method: setOption
# arguments:
# # #see https://symfony.com/blog/new-in-symfony-3-2-php-constants-in-yaml-files
# - !php/const Redis::OPT_PREFIX
Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler:
arguments:
- '#Redis'
# #see https://symfony.com/doc/current/components/yaml/yaml_format.html
-
prefix: ivannotes_

DIP - symfony 3.4 - parameter EntityManagerInterface always equal NULL

I not understand why the parameter is alway NULL
The project is blog basic on symfony 3.4.x (3.4.4)
I try cancel the cache but nothing has changed.
I update the versione from 3.4.3 to 3.4.4 but nothing has changed
something would be needed to check the container
class DefaultController extends Controller
{
/**
* #Route("/", name="homepage")
*/
public function indexAction(Request $request, EntityManagerInterface $em=null)
{
//$em === NULL
$a = $this->get('doctrine.orm.default_entity_manager');
var_dump($a);//this is valid entitymanger
// replace this example code with whatever you need
return $this->render('default/index.html.twig', [
'base_dir' => realpath($this->getParameter('kernel.project_dir')).DIRECTORY_SEPARATOR,
]);
}
}
My service YAML file is the original create with composer
# Learn more about services, parameters and containers at
# https://symfony.com/doc/current/service_container.html
parameters:
#parameter_name: value
services:
# default configuration for services in *this* file
_defaults:
# automatically injects dependencies in your services
autowire: true
# automatically registers your services as commands, event subscribers, etc.
autoconfigure: true
# this means you cannot fetch services directly from the container via $container->get()
# if you need to do this, you can override this setting on individual services
public: false
# makes classes in src/AppBundle available to be used as services
# this creates a service per class whose id is the fully-qualified class name
AppBundle\:
resource: '../../src/AppBundle/*'
# you can exclude directories or files
# but if a service is unused, it's removed anyway
exclude: '../../src/AppBundle/{Entity,Repository,Tests}'
# controllers are imported separately to make sure they're public
# and have a tag that allows actions to type-hint services
AppBundle\Controller\:
resource: '../../src/AppBundle/Controller'
public: true
tags: ['controller.service_arguments']

Do steel need register Twig Extensions in the services.yml?

I learning Symfony3 on https://knpuniversity.com. Now on create their own filters in Twig (https://knpuniversity.com/screencast/symfony-services/create-twig-extension). In this video the author creates a class filter, but when using this filter in the Twig template:
<dd>{{ genus.funFact|markdownify }}</dd>
Symfony crashes with the error, and the author of the video says that the filter has used need to say Symfony about new filter Twig'. This is done using the register Twig extension as service in the services.yml. But, I have it all working, without errors, without any registrations in service.yml.
Do steel need register Twig Extensions in the services.yml?
Or is now not required and determines its own Twig extension?
I have a Symfony 3.3.6 (in video used 3.0.0).
Here listing my service.yml:
parameters:
services:
_defaults:
autowire: true
autoconfigure: true
public: false
AppBundle\:
resource: '../../src/AppBundle/*'
exclude: '../../src/AppBundle/{Entity,Repository,Tests}'
AppBundle\Controller\:
resource: '../../src/AppBundle/Controller'
public: true
tags: ['controller.service_arguments']
AppBundle\Service\MarkdownTransformer:
arguments:
- '#markdown.parser'
- '#doctrine_cache.providers.my_markdown_cache'
public: true
# AppBundle\Twig\MarkdownExtension:
# arguments: ['#AppBundle\Service\MarkdownTransformer']
# tags:
# - { name: twig.extension }
# public: true
In the bottom I'm added the settings block, which registers a new TWIG extension. Without this block, Symfony throws an exception (in the author of the lesson). But I have everything working without this block. So for example, I have it commented out.

Categories