I try to get security service in an Entity.
When i want to access it in my entity, the property "$this->security" is null
See the entity :
<?php
namespace App\Entity\Production;
use Symfony\Component\Security\Core\Security;
/**
* #ORM\Entity(repositoryClass=MarqueRepository::class)
* #ORM\HasLifecycleCallbacks()
*/
class Marque
{
/* Others properties useless in the stackoverflow question*/
/**
* #var Security
*/
private $security;
public function __construct(Security $security)
{
$this->security = $security;
dd($this->security);
}
}
Autowiring is active in "services.yaml".
I removed the folder "Entity" in the "exclude src"
Could you help me ? Thxs
Bad practice.
As said in comment by Lunin Roman and Mcsky, security check could be made in service/controller/etc.
I was read some commentaries about this bad practice, unless that you just need retrive the currently user id logged. Then it is my case.
class Aaaa
private $security;
public function __construct(Security $security)
{
$this->security = $security;
}
}
services.yaml
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/Kernel.php'
- '../src/Tests/
so i got this error..
Cannot create an instance of App\Entity\Aaaaa from serialized data
because its constructor requires parameter "security" to be present.
Could by config error?
Related
I have a quick question regarding auto wiring on symfony.
I am currently migrating my symfony 2.8 app to symfony 5.4. I have my BaseManager class which is used by several manager of my application.
Several managers therefore use this class and pass it 2 arguments as parameters which are EntityManager and $class.
Of course, the $class variable is different for each manager and therefore cannot be managed by auto wiring.
So I have my managers that are declared as a service. However these declarations are not recognized by the auto wiring because I use an alias for these services. Is there a way to link my service alias to my manager without having to recreate several statements with the full name of the manager in the services.yml? this is a bit redundant, especially since I have about forty managers to transfer.
Thank you
This is my custom services.yml
# Employee Manager
WORD\EmployeeBundle\Service\EmployeeManager:
arguments:
- '#doctrine.orm.entity_manager'
- '%word.employee.model.employee.class%'
public: true
# Employee Manager
word.employee.manager.employee:
class: 'WORD\EmployeeBundle\Service\EmployeeManager'
arguments:
- '#doctrine.orm.entity_manager'
- '%word.employee.model.employee.class%'
public: true
This is my custom baseManager who expect $em and $class
abstract class BaseManager{
protected $em;
protected $repository;
protected $class;
public function __construct(EntityManagerInterface $em, string $class)
{
$this->em = $em;
$this->repository = $em->getRepository($class);
$metadata = $em->getClassMetadata($class);
$this->class = $metadata->name;
}
I have this deprecation message:
Since symfony/dependency-injection 5.1: The
"Symfony\Component\DependencyInjection\ContainerInterface" autowiring
alias is deprecated. Define it explicitly in your app if you want to
keep using it.
From threads such as this Symfony: Explicit define Container in Service I understand that the long-term solution is to stop using the ContainerInterface all together in my services.
My services.yaml looks like this:
parameters:
#locale: en
basepath: '%env(basepath)%'
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'
# controllers are imported separately to make sure services can be injected
# as action arguments even if you don't extend any base controller class
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
globalHelper:
class: App\Service\globalHelper
public: false
The service in question (globalHelper) looks like this:
<?php
namespace App\Service;
use Symfony\Component\DependencyInjection\ContainerInterface as Container;
use Doctrine\ORM\EntityManagerInterface as EntityManager;
class globalHelper {
private $container;
private $em;
public function __construct(Container $container, EntityManager $em) {
$this->container = $container;
$this->em = $em;
}
I only user the container to fetch session variables like this
$this->container->get('session')->getFlashBag()->add($type, $message);
And to get the current user (security context) like this
$this->container->get('security.context')->getToken()->getUser();
Can I get these sub-components of the container separately instead? What component then would I inject to access these two parts (session and user) respectively?
--------------- Addition --------------
According to Alexis' suggestion below I modified the head of the file with
<?php
namespace App\Service;
//use Symfony\Component\DependencyInjection\ContainerInterface as Container;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\Security;
use Doctrine\ORM\EntityManagerInterface as EntityManager;
class globalHelper {
//private $container;
private $requestStack;
private $security;
private $em;
//public function __construct(Container $container, RequestStack $requestStack, Security $security, EntityManager $em) {
public function __construct(RequestStack $requestStack, Security $security, EntityManager $em) {
//$this->container = $container;
$this->requestStack = $requestStack;
$this->security = $security;
$this->em = $em;
}
then replaced
$this->container->get('session')->getFlashBag()->add($type, $message);
with
$this->requestStack->getSession()->getFlashBag()->add($type, $message);
and get this error:
Attempted to call an undefined method named "getSession" of class
"Symfony\Component\HttpFoundation\RequestStack".
if I instead to this:
$this->requestStack->get('session')->getFlashBag()->add($type, $message);
Attempted to call an undefined method named "get" of class
"Symfony\Component\HttpFoundation\RequestStack". Did you mean to call
e.g. "getCurrentRequest", "getMasterRequest" or "getParentRequest"?
First it’s not mandatory to declare your service help with
autoconfigure: true
Then you must inject
Symfony\Component\HttpFoundation\RequestStack
and make
$requestStack->getSession()
Here's the docs
https://symfony.com/doc/current/session.html
For user you inject
Symfony\Component\Security\Core\Security
and make
$security->getUser()
Here's the docs
https://symfony.com/doc/current/security.html#fetching-the-user-from-a-service
-- EDIT --
Prio symfony 5.3 session can directly be injected with
Symfony\Component\HttpFoundation\Session\SessionInterface
It's depreciated after. Here's the blog post :
https://symfony.com/blog/new-in-symfony-5-3-session-service-deprecation
Using symfony 6.2, you can also add this on your service.yml:
Symfony\Component\HttpFoundation\Session\SessionInterface:
factory: "#=service('request_stack').getCurrentRequest()?.getSession()"
It can return null value
i started working on a new Symfony 6.0 project.
I created a new Entity called Project. In this entity I want to set the created_by property automaticlly on PrePersist (hook) call...
Therefore I created an AbstractEntity to extend the original Project entity.
In AbstractEntity I want to automatically inject Symfony\Component\Security\Core\Security service.
BUT the autowire stuff just doesn't work.
# config/services.yaml
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/' # --> i removed that line (doesnt work)
- '../src/Kernel.php'
#this also does not work
App\Entity\AbstractEntity:
autowire: true
#this also does not work
App\Entity\AbstractEntity:
arguments:
- '#security.helper'
// src/Entity/AbstractEntity.php
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\Security;
#[ORM\MappedSuperclass]
#[ORM\HasLifecycleCallbacks]
abstract class AbstractEntity
{
private $security;
public function __construct(Security $security)
{
$this->security = $security;
}
}
The entity should not have any dependencies and contain logic. If you want to do something, consider creating Doctrine Lifecycle Listeners prePersist or Doctrine Entity Listeners.
Lifecycle listeners are defined as PHP classes that listen to a single
Doctrine event on all the application entities.
Add to services.yaml file
App\EventListener\CreatedByLifecycleEvent:
tags:
-
name: 'doctrine.event_listener'
event: 'prePersist'
And create a listener
namespace App\EventListener;
use Doctrine\Persistence\Event\LifecycleEventArgs;
use Symfony\Component\Security\Core\Security;
class CreatedByLifecycleEvent
{
private $security;
public function __construct(Security $security)
{
$this->security = $security;
}
public function prePersist(LifecycleEventArgs $args): void
{
$entity = $args->getObject();
if(method_exists($entity,'setCreatedBy') and !empty($user = $this->security->getUser())){
$entity->setCreatedBy($user);
}
}
}
Thus, when saving any entity, provided that the setCreatedBy method exists, our listener will set the current user.
I have a problem autowiring some parameters in services config.
I can't clear:cache because it triggers the error so I don't think it is a cache issue.
And I have copy/paste all my files into a whole new Symfony project and still got the same problem.
This is my services.yaml
#config/services.yaml
parameters:
app.medipim_api_key_value: '%env(resolve:MEDIPIM_API_KEY)%'
app.medipim_api_key_id: 326
services:
[6 lines ...]
# 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,Entity,Migrations,Tests,Kernel.php}'
App\Services\MedipimApi:
arguments:
$medipim_api_key_id: '%app.medipim_api_key_id%'
$medipim_api_key_value: '%app.medipim_api_key_value%'
[10 lines ...]
This is my services :
#src/Services/MedipimApi.php
class MedipimApi
{
const FILTER_CNK_AND_ACTIVE = 'filter_cnk_and_active';
/**
* #var MedipimApiV3Client
*/
private MedipimApiV3Client $medipimApiV3Client;
private TranslatorInterface $translator;
/**
* MedipimApi constructor.
* #param string $medipim_api_key_id API key id for Medipim API call
* #param string $medipim_api_key_value Api key (secret) value for Medipim API call
* #param TranslatorInterface $translator TranslatorInterface object for I18N purpose
*/
public function __construct(string $medipim_api_key_id, string $medipim_api_key_value, TranslatorInterface $translator)
{
$this->translator = $translator;
$this->medipimApiV3Client = new MedipimApiV3Client($medipim_api_key_id, $medipim_api_key_value);
}
....
When I try to call this service, I got this error :
Cannot autowire service "App\Services\MedipimApi": argument
"$medipim_api_key_id" of method "__construct()" is type-hinted
"string", you should configure its value explicitly.
It is working only when I copy the value of services.yaml and paste it into my services_env.yaml (where env is my current environment).
#config/services_dev.yaml
parameters:
app.medipim_api_key_value: '%env(resolve:MEDIPIM_API_KEY)%'
app.medipim_api_key_id: 326
services:
[6 lines ...]
# 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,Entity,Migrations,Tests,Kernel.php}'
App\Services\MedipimApi:
arguments:
$medipim_api_key_id: '%app.medipim_api_key_id%'
$medipim_api_key_value: '%app.medipim_api_key_value%'
[10 lines ...]
Now it works!
Why did that work?
Symfony is suppose to load file as described here :
For the dev environment, Symfony loads the following config files and
directories and in this order:
config/packages/*
config/packages/dev/*
config/services.yaml
config/services_dev.yaml
What did I miss?
EDIT
My kernel class looks right. It is loading services.yaml and then services_env.yaml as it should be :
class Kernel extends BaseKernel
{
[1 line...]
protected function configureContainer(ContainerConfigurator $container): void
{
$container->import('../config/{packages}/*.yaml');
$container->import('../config/{packages}/'.$this->environment.'/*.yaml');
if (is_file(\dirname(__DIR__).'/config/services.yaml')) {
$container->import('../config/{services}.yaml');
$container->import('../config/{services}_'.$this->environment.'.yaml');
} elseif (is_file($path = \dirname(__DIR__).'/config/services.php')) {
(require $path)($container->withPath($path), $this);
}
}
[14 lines...]
Why you configure param xx_id as int in your yml file ? In your service you type-hinted id as string !
Short story : I have some diffulties to use a listener properly on my custom "vendor" bundle.
I'm coding a reusable bundle for Symfony 4.1 framework (for managing Users). I put the whole bundle in this location of a new symfony project : myproject/lib/AcmeUserBundle/src/
Just like this Demo
Because my bundle is at a non usual place, I changed my config/services.yaml like this (in case this is relevent) :
services:
# default configuration for services in *this* file
_defaults:
autowire: true
autoconfigure: true
public: false
App\:
resource: '../src/*'
exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
App\Controller\:
resource: '../src/Controller'
tags: ['controller.service_arguments']
Acme\UserBundle\:
resource: '../lib/AcmeUserBundle/src/*'
exclude: '../lib/AcmeUserBundle/src/{DependencyInjection,Entity,Migrations,Tests}'
Acme\UserBundle\Controller\:
resource: '../lib/AcmeUserBundle/src/Controller'
tags: ['controller.service_arguments']
Then, I coded a login form (that you don't care much), and when a user logged in, I want to update his last datetime connexion info in database. So I have built a listener following this tutorial on the security.interactive_login event :
<?php
namespace Acme\UserBundle\EventListener;
use DateTime;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
class LoginListener
{
/**
* #var EntityManagerInterface
*/
private $entityManager;
/**
* LoginListener constructor.
*
* #param EntityManagerInterface $entityManager
*/
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
/**
* #param InteractiveLoginEvent $event
*/
public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
{
// Get the User entity.
$user = $event->getAuthenticationToken()->getUser();
/** #var \Acme\UserBundle\Entity\User $user */
$user->setLastLoggedAt(new DateTime());
$this->entityManager->persist($user);
$this->entityManager->flush();
}
}
lib/AcmeUserBundle/src/Resources/config/services.yaml looks like :
services:
Acme\UserBundle\EventListener\LoginListener:
tags:
- { name: kernel.event_listener, event: security.interactive_login }
lib/AcmeUserBundle/src/DependencyInjection/AcmeUserExtension.php looks like :
class AcmeUserExtension extends Extension
{
/**
* #param array $configs
* #param ContainerBuilder $container
*/
public function load(array $configs, ContainerBuilder $container)
{
$loader = new YamlFileLoader(
$container,
new FileLocator(__DIR__.'/../Resources/config')
);
$loader->load('services.yaml');
}
}
I can see my service using these symfony commands :
php bin/console debug:autowiring
php bin/console debug:container
But I can't see anything with :
php bin/console debug:event-dispatcher
When I log-in with my user, no error occurs, and of course, no date is inserted in database. That's why I think my listener is not properly registered.
Any idea why ?
EDIT after Cerad's comment :
Output of php bin/console debug:container LoginListener
---------------- ------------------------------------------------
Option Value
---------------- ------------------------------------------------
Service ID Acme\UserBundle\EventListener\LoginListener
Class Acme\UserBundle\EventListener\LoginListener
Tags -
Public no
Synthetic no
Lazy no
Shared yes
Abstract no
Autowired yes
Autoconfigured yes
---------------- ------------------------------------------------
We can see that the Tags part is empty. That's the reason why my listener is ignored.
And... it works if I comment that part of my config/service.yaml :
...
#Acme\UserBundle\:
# resource: '../lib/AcmeUserBundle/src/*'
# exclude: '../lib/AcmeUserBundle/src/{DependencyInjection,Entity,Migrations,Tests}'
...
New output of php bin/console debug:container LoginListener
---------------- -----------------------------------------------------------
Option Value
---------------- -----------------------------------------------------------
Service ID Acme\UserBundle\EventListener\LoginListener
Class Acme\UserBundle\EventListener\LoginListener
Tags kernel.event_listener (event: security.interactive_login)
Public no
Synthetic no
Lazy no
Shared yes
Abstract no
Autowired no
Autoconfigured no
---------------- -----------------------------------------------------------
Tags is now correct and my listener works properly