我正在尝试创建一个服务,该服务接收.env文件中定义的两个参数。
Symfony抛出下一个错误:
无法自动连线服务 “ App \ Modules \ Sales \ Infrastructure \ Persistence \ Http \ HttpSalesRepository”: 方法“ __construct()”的参数“ $ credentials”的类型为“ ar
ray”,则应明确配置其值。
这是我的service.yml文件:
parameters:
services:
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
App\:
resource: '../src/*'
exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
App\Controller\:
resource: '../src/Controller'
tags: ['controller.service_arguments']
imports:
- { resource: "../src/Modules/Sales/Infrastructure/DependencyInjection/sales_module.yml" }
这是sales_module.yml中的代码:
services:
module_sales.http.sales.repository:
public: false
class: App\Modules\Sales\Infrastructure\Persistence\Http\HttpSalesRepository
arguments: ["%env(json:CREDENTIALS)%", "%env(json:TAGS)%"]
这是HttpSalesRepository类
class HttpSalesRepository implements SalesRepository
{
private $credentials;
private $tags;
public function __construct(array $credentials, array $tags)
{
$this->credentials = $credentials;
$this->tags = $tags;
}
...
}
这是.env文件:
###> symfony/framework-bundle ###
APP_ENV=dev
APP_SECRET=xxxxxx
###< symfony/framework-bundle ###
###> doctrine/doctrine-bundle ###
DATABASE_URL=mysql://xxx:yyy@mysql:3306/db
###< doctrine/doctrine-bundle ###
###> application/variables ###
CREDENTIALS='{"username":"email@test.com","password":"secretpass"}'
TAGS='["tag1","tag2","tag3"]'
###< application/variables ###
有什么想法吗?
答案 0 :(得分:0)
您可以命名服务参数以使参数名称与值显式匹配,而不仅仅是参数列表:
services:
module_sales.http.sales.repository:
public: false
class: App\Modules\Sales\Infrastructure\Persistence\Http\HttpSalesRepository
arguments:
$credentials: "%env(json:CREDENTIALS)%"
$tags: "%env(json:TAGS)%"]
答案 1 :(得分:0)
这不是最佳选择,但是当我找到最终解决方案时,这是一项工作。如果我在config / services.yaml中定义服务,则变量将完美地传递到服务。
App\Modules\Sales\Infrastructure\Persistence\Http\HttpSalesRepository:
public: false
arguments: ["%env(json:AMAZON_AFFILIATES_CREDENTIALS)%", "%env(json:AMAZON_AFFILIATES_TAGS)%"]
任何其他解决方案都将受到欢迎。