Failed to pass parameter to Symfony 3.3 service

I use a parameter in my service with Symfony 3.3, but I get an error all the time.

Error

[Symfony \ Component \ dependency injection \ Exception \ AutowiringFailedException] Unable to audit the service "AppBundle \ Service \ ApiInterface": the argument "api_endpoint" of the method "__construct ()" must have a hint type or explicitly specify a value.

config.yml

services: app.security.login_form_authenticator: class: AppBundle\Security\LoginFormAuthenticator autowire: true arguments: ['@doctrine.orm.entity_manager'] app.service.api_interface: class: AppBundle\Service\ApiInterface arguments: $api_endpoint: "%endpoint test%" _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'] 

ApiInterface.php

 use Unirest; class ApiInterface { private $api_endpoint; public function __construct(string $api_endpoint) { $this->timeout = 1; echo 'form apiinterface construct: ' . $api_endpoint; } 

Any grateful help feels like I'm gathering in circles in what should be a simple job!

+5
source share
1 answer

The problem is that you have 2 different services: app.service.api_interface and AppBundle\Service\ApiInterface . The first is good, the second is not.

If you need the service app.service.api_interface , you can change your configuration to have the first as an alias of the second type:

 app.service.api_interface: '@AppBundle\Service\ApiInterface' AppBundle\Service\ApiInterface: arguments: $api_endpoint: "%endpoint test%" 

In your configuration, you do not configure the AppBundle\Service\ApiInterface , but you configure the app.service.api_interface service. At my suggestion, you will configure 2 services.

If you do not need the service app.service.api_interface , you can only allow one service:

 AppBundle\Service\ApiInterface: arguments: $api_endpoint: "%endpoint test%" 

This declaration overrides the AppBundle\Service\ApiInterface . You can override any service that is imported using your identifier (class name) below: It is preferable to move this override below the AppBundle\ declaration.

The final file may be like this:

 #app/config/services.yml 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'] app.security.login_form_authenticator: '@AppBundle\Security\LoginFormAuthenticator' # autowire: true #Optional, already set in _defaults # arguments: ['@doctrine.orm.entity_manager'] # Optional because of autowiring app.service.api_interface: '@AppBundle\Service\ApiInterface' AppBundle\Service\ApiInterface: arguments: $api_endpoint: "%endpoint test%" 

Documentation: Manual Posting Arguments

Documentation: explicit configuration of services and arguments

In addition, I suggest deleting the parameter %endpoint test% (and renaming it, for example, to %endpoint_test% )

+2
source

Source: https://habr.com/ru/post/1272524/


All Articles