I try configure my services in Symfony 5.5 with tags and a resource folder and I also used some different notations. Either I got an empty iterator as constructor param or the exception "Cannot autowire service ... argument "..." of method "__construct()" is type-hinted "iterable", you should configure its value explicitly.".
I used that easy feature in previous versions and I followed that instruction: https://symfony.com/doc/current/service_container/tags.html#reference-tagged-services.
Here that related part of my services.yaml:
services:
_defaults:
autowire: true
autoconfigure: true
App\Service\LinkTypeGuesser\:
resource: '../src/Service/LinkTypeGuesser'
tags: ['link.type.guesser']
App\Service\LinkTypeGuesser:
arguments:
- !tagged_iterator link.type.guesser
My "parent" service class constructor looks like that:
class LinkTypeGuesser
{
private $guessers;
public function __construct(iterable $linkTypeGuessers)
{
$this->guessers = $linkTypeGuessers;
}
}
Any hints what I missed in my configuration?
I don't really know your repository therefor the file system.
First of all the argument resource is not supported.
Supported arguments are "shared", "lazy", "public", "properties", "configurator", "calls", "tags", "autowire", "bind"
resource: '../src/Service/LinkTypeGuesser'
Symfony will find your resources based on the Fully Qualified Namespace, you have set as an key. This means everything that implements or extends this Service will be tagged.
You want to add a custom tag and inject all services in your parent service.
You have a syntax error, the argument _instanceof is missing.
GO ahead and try this code (Please change whitespaces to match yaml format):
services:
_defaults:
autowire: true
autoconfigure: true
_instanceof:
App\Service\LinkTypeGuesser:
tags: ['link.type.guesser']
App\Service\LinkTypeGuesser:
arguments:
- !tagged_iterator link.type.guesser
Related
I would like to have an ability to create services by theirs class-names.
One of the way to do it: is setting "public" property in "services.yaml"
BUT I DON'T WANT to set "public" property for ALL classes in my project
services.yaml
services:
_defaults:
public: false # it helps to optimize performance, doesn't it?
App\Service\Service1
public: true
App\Service\Service2
public: true
App\Service\Service3
public: true
App\Service\* # why I can't use something like "*" here ???
public: true
Service1.php
namespace App\Service;
class Service1
{
// important: every service can have one or more dependencies (Foo, Bar, Baz ... etc)
public function __construct(Foo $foo, Bar $bar)
{
$this->foo = $foo;
//..........
}
}
MyController.php
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
// hier $className is any class name like "App/Service/xxxx"
public function myAction (string $className)
{
return $this->container->get($className);
}
Questions:
is there a way to set "public" property for directory?
is there a better way to create an instance of service by class-name?
Thanks
Perhaps you can utilize YAML import features to generalize the configuration that you need, making it easier to maintain.
For example:
# /config/services.yaml
imports:
- { resource: 'services/public/*.yaml' } # Import public services
- { resource: 'services/private/*.yaml' } # Import private services
Though, as mentioned, forcing container services to be public goes against Symfony conventions, and will not be supported in future versions. Use dependency injection instead of interacting with the service container directly.
Yes, this should be possible. I tested it with Symfony 5.3.
See the last part in this 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.
bind:
$projectDir: '%kernel.project_dir%'
# 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/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
App\Messenger\Handler\Event\:
resource: '../src/Messenger/Handler/Event/'
public: true
Similar to controllers you can include multiple classes via resource and make them public. But please be aware of the other answers/comments. Usually you should use dependency injection to load the services you need.
Alternatively (or even better) you could set a tag and use an iterable via dependency injection. See https://symfony.com/doc/5.3/service_container/tags.html#reference-tagged-services for more information.
Why would you need to set services to public? It goes against Symfony best practices which discourage fetching services from the container directly.
If you wish to define services named by their class name, it is enough to reference them with:
services:
App\Service\Service1: ~
App\Service\Service2: ~
App\Service\Service3: ~
App\Service\Service4:
arguments:
- '#App\Service\Service1'
- '#App\Service\Service2'
- '#App\Service\Service3'
and then inject them to your controllers directly, instead of using the container.
My config/service.yaml contains several services that have huge configuration. Now, I want to move the configuration of that services to a separate file.
I tried to do like this:
At the end of service.yaml:
imports:
- { resource: 'packages/custom_service.yaml' }
config/packages/custom_service.yaml:
services:
App\Service\CustomService:
arguments:
$forms:
- location: 'room1'
id: 43543
- location: 'room2'
id: 6476546
- location: 'room3'
id: 121231
...
src/Service/CustomService.php:
/**
* #var array
*/
protected $forms;
public function __construct(array $forms)
{
$this->forms = $forms;
}
But when I try to autowire in some Controller, I am getting this error:
Cannot resolve argument $customService of "App\Controller\CustomController::customAction()": Cannot autowire service "App\Service\CustomService": argument "$forms" of method "__construct()" is type-hinted "array", you should configure its value explicitly.
But if I remove type hint, Then i get this error:
Cannot resolve argument $customService of "App\Controller\CustomController::customAction()": Cannot autowire service "App\Service\CustomService": argument "$forms" of method "__construct()" has no type-hint, you should configure its value explicitly.
The other two answers are partially correct but they don't quite cover everything.
To start with, anytime you decide you want multiple autowired service files you have to be careful that a given service is only autowired by one and only one file. Otherwise Symfony will attempt to define the service multiple times and usually fail. There are different approaches to accomplishing this but the most straight forward one is to explicitly exclude any custom services your config/services.yaml file:
# config/services.yaml
parameters:
imports:
- { resource: 'services/custom_service.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/Service/CustomService.php' # ADD THIS
- '../src/DependencyInjection/'
- # the rest of the default excludes
So that takes care of skipping CustomService.php in the main config file.
Secondly, notice in the above file I loaded the custom_service.yaml file from config/services instead of config/packages. The packages is really reserved for bundle specific configuration. Every file in there is loaded by default. Putting service files in there may or may not work but it definitely changes the loading order of things and could cause problems. So make a directory just for custom services and use it.
You also need to repeat the _defaults section in each service file since _defaults are considered to be file specific. Bit of a pain perhaps but that is the way it is:
# config/services/custom_service.yaml
services:
_defaults:
autowire: true
autoconfigure: true
App\Service\CustomService:
arguments:
$forms:
-
location: 'room1'
id: 43543
-
location: 'room2'
id: 6476546
-
location: 'room3'
id: 121231
Also note that your yaml array syntax was messed up. Possibly just a copy/paste issue.
And that should do it.
Autowiring has file scope.
You need to set it in config/packages/custom_service.yaml as well.
Modify that file as follows
services:
_defaults:
autowire: true
App\Service\CustomService:
[...]
You should also add autoconfigure and/or visibility (public keyword) if needed.
Unfortunately, I cannot add a comment so I have to use an answer.
I have to make the following assumption, I believe you have the following snippet inside your services.yaml:
App\:
resource: '../src/'
exclude:
- '../src/DependencyInjection/'
- '../src/Entity/'
- '../src/Kernel.php'
- '../src/Tests/'
which already includes your desired class App\Service\CustomService and so Symfony will try to autowire it before looking inside the imports which leads to the error because you have that class defined inside the import which will only be evaluated if the root services.yaml does not already find the class.
The following example will work. However, you will lose auto-discovery of classes inside the App namespace and it is not recommended to do this.
services.yaml:
parameters:
imports:
- { resource: 'custom_services.yaml' }
services:
# default configuration for services in *this* file
_defaults:
autowire: true
autoconfigure: true
App\Controller\:
resource: '../src/Controller/'
tags: ['controller.service_arguments']
custom_services.yaml:
services:
_defaults:
autowire: true
autoconfigure: true
App\Service\SomeService:
arguments:
$forms:
- location: 'room1'
id: 43543
- location: 'room2'
id: 6476546
- location: 'room3'
id: 121231
src/Service/SomeService.php:
<?php
declare(strict_types=1);
namespace App\Service;
class SomeService
{
protected array $forms;
public function __construct(array $forms)
{
$this->forms = $forms;
}
public function getForms()
{
return $this->forms;
}
}
src/Controller/HomeController:
<?php
declare(strict_types=1);
namespace App\Controller;
use App\Service\SomeService;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
class HomeController
{
#[Route('/', 'home')]
public function __invoke(SomeService $someService)
{
return new JsonResponse($someService->getForms());
}
}
What you can do instead is import the file in the Kernel instead in the yaml file which will overwrite the service definition.
So remove the imports part inside your services.yaml and make sure that you import the file inside the src/Kernel.php:
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');
// Import your custom service files after services.yaml to overwrite already existing definitions
$container->import('../config/custom_services.yaml');
$container->import('../config/{services}_'.$this->environment.'.yaml');
} elseif (is_file($path = \dirname(__DIR__).'/config/services.php')) {
(require $path)($container->withPath($path), $this);
}
}
I have the following service definition in Symfony2
app.service_1:
class: Symfony\Component\Lock\Store\RedisStore
arguments:
- '#snc_redis.default_cache'
app.service_2:
class: Symfony\Component\Lock\Store\RedisStore
arguments:
- '#snc_redis.scheduler_cache'
Now I'm planning to upgrade to Symnfony4 where I need to give classpath as the service name
Symfony\Component\Lock\Store\RedisStore
arguments:
- '#snc_redis.default_cache'
Symfony\Component\Lock\Store\RedisStore
arguments:
- '#snc_redis.scheduler_cache'
Here the problem is it has the same name because we use the same classpath? How I can fix it? Can I use an alias with different parameters?
You do not need to change the definition.
When you need to create several services from the same class, using the FQCN as an identifier won't work. Using the fully qualified class name is recommended and a good practice, but it's not mandatory. It's practical most of the time, since you can omit the class argument, and you do not need to pick a name for each service.
Your original definition is perfectly compatible with Symfony 4 (or 5):
app.service_1:
class: Symfony\Component\Lock\Store\RedisStore
arguments:
- '#snc_redis.default_cache'
app.service_2:
class: Symfony\Component\Lock\Store\RedisStore
arguments:
- '#snc_redis.scheduler_cache'
I would simply advise to use more descriptive identifiers than service_1 and service_2.
An other way is to use alias like explain in documentation
services:
Symfony\Component\Lock\Store\RedisStore:
public: false
arguments:
- '#snc_redis.default_cache'
app.service_1:
alias: Symfony\Component\Lock\Store\RedisStore
public: true
Symfony\Component\Lock\Store\RedisStore:
arguments:
- '#snc_redis.scheduler_cache'
app.service_2:
alias: Symfony\Component\Lock\Store\RedisStore
public: true
You can also use bind and autowire: true for your arguments. But your variables in services.yml must be same as them declared in constructor of yours services.
Look something like this:
services:
# default configuration for services in *this* file
_defaults:
autowire: true
autoconfigure: true
bind:
$defaultCache: '#snc_redis.default_cache'
$schedulerCache: '#snc_redis.scheduler_cache'
Symfony\Component\Lock\Store\RedisStore:
public: false
(or use bind only in your service declaration).
PS: caution in your example, you miss : after class path :)
In file service.yaml i have:
parameters:
security.allows.ip:
- '127.0.0.1'
- '127.0.0.2'
Or:
parameters:
security.allows.ip: ['127.0.0.1', '127.0.0.2']
And configuration for DI:
services:
_defaults:
autowire: true
autoconfigure: true
public: false
And i want to configure service for class:
security.class:
class: App\Class
arguments:
- '%security.allows.ip%'
And finally I have message:
Cannot autowire service "App\Class": argument "$securityConfiguration" of method "__construct()" must have a type-hint or be given a value explicitly.
And constructor definition is:
public function __construct(array $securityConfiguration)
Could you help me with it? In symfony 2.8 it works, but for this configuration I have this error. Other sevices for type hint string is ok, but not for this class. If I add container interface to construct for this class and getting parameter by ->getParameter('security.allows.ip') it works. Why?
In order for autowire to work, the typehint need to match a service id. The problem here is that you have another class into which you are trying to inject your rather poorly named App\Class
class SomeOtherClass {
public function __construct(App\Class $appClass)
When you created your AppClass service, you gave it an id of security.class. So autowire looks for a service id of App\Class, does not find it and then attempts to create one. And of course it cannot autowire an array.
One way to fix this is by using an alias:
security.class:
class: App\Class
arguments:
- '%security.allows.ip%'
App\Class: '#security.class'
A second (recommended) approach is to do away with the security.class id completely
App\Class:
arguments:
- '%security.allows.ip%'
And if you really want to be the cool kid on the block, you can even drop the arguments keyword.
App\Class:
$securityConfiguration: '%security.allows.ip%'
I am writing a service in Symfony 3 according to latest docs.
I wrote a class:
<?php
namespace julew\AdminBundle\Service;
class FileService {
public function create() {
die('I am here!');
}
}
My app/config/services.yml looks like:
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
julew\AdminBundle\:
resource: '../../src/julew/AdminBundle/*'
exclude: '../../src/julew/AdminBundle/{Entity,Repository}'
But only what i get is this error:
Class Symfony\Bundle\FrameworkBundle\Test\WebTestCase not found in C:\xampp\htdocs\roch\app/config\services.yml (which is being imported from "C:\xampp\htdocs\roch\app/config\config.yml").
What am i doing wrong?
EDIT 1
The problem was, that automatically generated bundles had tests in it - had to add them to exclude paths.
But now other problem occured. Service is not injected via controler type-hint. Error that i get:
Controller "julew\RochNotesBundle\Controller\ParserController::indexAction()" requires that you provide a value for the "$fileService" argument. Either the argument is nullable and no null value has been provided, no default value has been provided or because there is a non optional argument after this one.
The controller method looks like:
public function indexAction(FileService $fileService) {
.
.
.
}
And at the top i added:
use julew\AdminBundle\Service\FileService;
If you want to inject various services into a controller, it can be a little easier to do so in the constructor, and avoid the use of the controller.service_arguments tag (which has to be explicitly added for the controller definition in the services.yml).