如何将动态变量从控制器传递到服务?我想在我的服务的构造器中管理一些依赖json值的距离。我的服务在构造中有两个参数:一个服务和一个带有JSON的变量。
对于第一个,我直接在service.yaml中传递了它。对于第二个,我有一些困难。
在控制器中,我从API获取JSON。但是这个json可以是空的
class IndexController extends AbstractController
{
/**
* @Route("/converter-hl7", name="converter", methods={"POST"})
*/
public function index(Request $request, $myjson = null) {
$myjson = $request->getContent();
global $kernel;
$converter = $kernel->getContainer()->get('app.converter');
$xml = $converter->outputXML();
$response = new Response($xml);
$response->headers->set('Content-Type', 'xml');
return $response;
}
我将JSON存储在我的。env文件中,MYJSON=null。这是我的服务
# This file is the entry point to configure your own services.
# Files in the packages/ subdirectory configure your dependencies.
# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
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/DependencyInjection/'
- '../src/Entity/'
- '../src/Kernel.php'
- '../src/Tests/'
# controllers are imported separately to make sure services can be injected
# as action arguments even if you don't extend any base controller class
AppController:
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
# explicitly configure the service
app.error:
class: AppServiceError
public: true
autowire: true
app.converter:
class: AppServiceConverterHl7Refacto
public: true
autowire: true
arguments:
$error: '@app.error'
$json: '%env(MYJSON)'
因此,在我的服务ConverterHl7Refacto.php中,我在构造函数中有两个参数。我想管理的立场,如果json是空的或非。如果我做$json的dd(),我得到'%env(MYJSON)'而不是json。为什么?
class ConverterHl7Refacto
{
private $ricetta;
private $identificativiDocumento;
private $codiceDocumento;
private $infoDocumento;
private $assistiti;
private $partecipanti;
private $relatedDoc;
private $structuredBody;
private $root;
private $error;
public function __construct(string $json,Error $error) {
$this->error = $error;
if ($json){
$this->ricetta = new Ricetta(json_decode($json));
$this->root = new Root();
$this->identificativiDocumento = new Identificativi();
$this->codiceDocumento = new CodiceDocumento($this->ricetta);
$this->infoDocumento = new InfoDocumento($this->ricetta);
$this->assistiti = new Assistiti($this->ricetta);
$this->partecipanti = new Partecipanti($this->ricetta);
$this->relatedDoc = new RelatedDocument($this->ricetta);
$this->structuredBody = new StructuredBody($this->ricetta);
}
}
工厂概念没有什么特别棘手的。工厂基本上用于创建给定类型的实例。我没有测试下面的代码,所以为拼写错误道歉:
class Error {}
class Converter {
public function __construct(string $json, Error $error)
{
// Whatever
...
class ConverterFactory {
private $error;
public function __construct(Error $error) {
$this->error = $error;
}
public function create(string $json) : Converter {
return new Converter($json,$this->error);
}
}
class MyController {
public function action(ConverterFactory $converterFactory)
{
$json = $request->getContent();
$converter = $converterFactory->create($json);
您需要在您的服务中从自动连接中排除您的Converter类。yaml文件。但这应该是你所需要的。不需要显式地定义像app.converter这样的东西,因为autowire会处理所有这些。