问题
I'm trying to use imports in services.yaml to move some services definitions to separate file. Here you can find this in symfony doc.
But when i'm trying to do this I'm getting an error:
Cannot autowire service "App\Domain\Test\Test": argument "$param" of method "__construct()" must have a type-hint or be given a value explicitly.
Here are my config files:
# services.yaml
imports:
- { resource: services/test.yaml }
services:
_defaults:
autowire: true
autoconfigure: true
public: false
App\:
resource: '../src/*'
exclude: '../src/{Entity,Migrations,Tests,Kernel.php}'
App\Controller\:
resource: '../src/Controller'
tags: ['controller.service_arguments']
And
# test.yaml
parameters:
param: 'some param'
services:
App\Domain\Test\Test:
public: true
autowire: true
arguments:
$param: '%param%'
Here is the service:
namespace App\Domain\Test;
class Test
{
private $mailer;
private $param;
public function __construct(\Swift_Mailer $mailer, string $param)
{
$this->mailer = $mailer;
$this->param = $param;
}
}
This is working well if I move parameters and service into services.yaml. Do somebody know what I'm doing wrong?
回答1:
The problem is that your first file try to load as service your service and can't fill automatically the parameter string $param
.
A good fix may be to exclude your service:
services:
App\:
resource: '../src/*'
exclude: '../src/{Entity,Migrations,Tests,Kernel.php,Domain/Test.php}'
Another solution is to simply put this definition inside the services.yaml
file.
You may also consider to load App\Something\:
instead of the complete namespace but this has drawbacks if you try to autowire in cross-namespaces definitions. (some features may not work as expected)
来源:https://stackoverflow.com/questions/49782392/external-service-configuration-in-symfony-4