Symfony 6 - cannot resolve argument - php

I've created a class App\Service\Steam\SteamAPIRepository and it is listed when I run bin/console debug:container. My class constructor looks like this:
public function __construct(HttpClientInterface $httpClient, CacheInterface $cache, string $apiKey)
{
$this->setCache($cache);
$this->setHttpClient($httpClient);
$this->setApiKey($apiKey);
$this->setSteamUrl('http://api.steampowered.com/');
}
Before I added the $apiKey argument (and just had it hardcoded in the constructor), this was working fine and could be autowired in a controller method for example. I added the API key argument and inside config/services.yaml I did the following:
parameters:
steam.apiKey: 'myapikey'
services:
# after _defaults and App
App\Service\Steam\SteamAPIRepository\:
resource: '../src/Service/Steam/SteamAPIRepository.php'
bind:
$apiKey: '%steam.apiKey%'
But now I see the error:
Cannot resolve argument $steamAPIRepository of "App\Controller\App\IndexController::index()":
Cannot autowire service "App\Service\Steam\SteamAPIRepository":
argument "$apiKey" of method "__construct()" is type-hinted "string",
you should configure its value explicitly.
Any ideas where I'm going wrong?

I figured this out, I was making quite a simple mistake inside config/services.yaml. I replaced:
App\Service\Steam\SteamAPIRepository\:
resource: '../src/Service/Steam/SteamAPIRepository.php'
bind:
$apiKey: '%steam.apiKey%'
with:
App\Service\Steam\SteamAPIRepository:
arguments:
$apiKey: '%steam.apiKey%'
Now this works as intended

Related

Symfony - argument is type-hinted "array" you should configure its value explicitly

In my Symfony 5.4 I get an error:
Cannot autowire service "App\Core\Shared\Query\CollectionQueryFactory": argument "$collectionOptions" of method "__construct()" is type-hinted "array", you should configure its value explicitly.
I wanted to explicitly configure what Symfony DI should pass. Tried with tagged iterator.
class CollectionQueryFactory implements CollectionQueryFactoryInterface
{
private array $collectionOptions;
public function __construct(array $collectionOptions)
{
$this->collectionOptions = $collectionOptions;
}
...
And in my services.yaml file:
App\Core\Shared\Query\CollectionQueryFactory:
arguments:
$collectionOptions: '%api_platform.collection.pagination%'

Reference Tagged Services cannot be resolved

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

Symfony 3.4 - Auto wire of service not working in method

I'm relatively new to Symfony, and I'm having trouble some trouble.
I'm trying to type hint a custom RequestValidator class in the method being called when the endpoint is called.
Using Symfony 3.4
However, I am getting the following error:
Controller "ApiBundle\Endpoints\Healthcheck\v1\Index::check()" requires that you provide a value for the "$request" 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.
Here is my setup:
services.yml file
...
_defaults:
autowire: true
autoconfigure: true
...
routing.yml
api.Healthcheck:
path: /healthcheck
controller: ApiBundle\Endpoints\Healthcheck\v1\Index::check
defaults: { _format: json }
methods:
- GET
And then - inside the Index class, I have the following:
<?php
namespace ApiBundle\Endpoints\Healthcheck\v1;
use ApiBundle\Responses\ApiResponse;
class Index extends ApiResponse
{
public function check(HealthcheckRequest $request) {
var_dump($request);die;
}
}
When I do debug:autowiring I see my HealthcheckRequest in the list.
Further, when I do the same and try type-hint in the constructor of the Index class, it all works.
And finally, if I try and type hint the Symfony/HttpFoundation/Request, inside the check() method, it instantiates it correctly.
In summary:
Not working :
check(HealthcheckRequest $request)
Working:
__construct(HealtcheckRequest $request)
check(SymfonyRequest $request)
Am I doing something wrong? Any help is appreciated.
It's part of services.yaml already in Symfony 4, but introduced in version 3.3, so this might help:
# controllers are imported separately to make sure services can be injected
# as action arguments even if you don't extend any base controller class
ApiBundle\Endpoints\:
resource: '../../Endpoints/*'
tags: ['controller.service_arguments']

Symfony 4 container array service parameter

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%'

how to use a service inside another service in symfony 2.6

I have a service setup in symfony 2.6 by name ge_lib and it looks like below
ge_lib:
class: GE\RestBundle\Services\GELib
arguments: [#session, #doctrine.orm.entity_manager, #manage_ge_proc]
inside GELib.php I have a requirement to use a function from another service manage_ge_proc
manage_ge_proc:
class: GE\RestBundle\Services\GEManageProcedure
arguments: [#doctrine.orm.entity_manager, #manage_ge_native_query]
if I try to use it this way, it is not working
$emailInv = $this->get('manage_ge_proc');
$sendStatus = $emailInv->pSendGeneralEmail(.....);
It gives error saying that unable to find any get function by that name. generally this -> $this->get('manage_ge_proc');works in any controller.But how do i use it in service?.
I tried $this->getContainer()->get('manage_ge_proc'); but it did not work.
This call is fetching service from DI container, which you dont have in your service
$this->get('manage_ge_proc');
It works in controller because DI container is automatically injected there.
Since you have this line in you services.yml, which tells Symfony to inject #manage_de_proc service into ge_lib constructor
arguments: [#session, #doctrine.orm.entity_manager, #manage_ge_proc]
you should be able to pick #manage_ge_proc from constructor like this:
public function __construct(
Session $session,
EntityManager $entityManager,
GEManageProcedure $manageGeProc
)
{
//... whatever you do in your constructor
$this->manageGeProc = $manageGeProc;
}

Categories