Answer the question
In order to leave comments, you need to log in
How to create a service for a class and pass Symfony 4 arguments there?
I have a class
class ExistsChecker
{
/**
* @var Repository
*/
private $repository;
/**
* @var UniqueEntity
*/
private $uniqueEntity;
public function __construct(
UniqueEntityInterface $uniqueEntity,
Repository $repository
) {
$this->repository = $repository;
$this->uniqueEntity = $uniqueEntity;
}
}
user_exists_checker:
class: App\ExistsChecker
arguments:
App\Repository\Repository: '@App\Repository\User\DoctrineUserRepository'
App\Entity\UniqueEntityInterface: '@App\Entity\User'
Answer the question
In order to leave comments, you need to log in
https://symfony.com/doc/current/service_container/...
services:
_defaults:
autowire: true
autoconfigure: true
bind:
App\Repository\Repository $cachedRepository: '@App\Repository\User\CachedUserRepository'
App\Repository\Repository: '@App\Repository\User\DoctrineUserRepository'
App\Entity\UniqueEntityInterface: '@App\Entity\User'
user_exists_checker:
class: App\ExistsChecker
bind:
App\Repository\Repository: '@App\Repository\User\ApiUserRepository'
user_exists_checker:
class: App\ExistsChecker
bind:
App\Repository\Repository: '@App\Repository\User\ApiUserRepository'
_defaults:
autowire: true
autoconfigure: true
bind:
App\Repository\Repository $cachedRepository: '@App\Repository\User\CachedUserRepository'
App\Repository\Repository $repository
you use instead of in the constructor App\Repository\Repository $cachedRepository
, you will getApp\Repository\User\CachedUserRepository
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question