नियंत्रक से किसी सेवा में गतिशील चर कैसे पास कर रहा है? मैं अपनी सेवा के निर्माता में कुछ दूरी का प्रबंधन करना चाहता हूं जो कि जेसन वैल्यू पर निर्भर करता है। मेरी सेवा निर्माण में दो पैरामीटर लेती है: एक सेवा और JSON के साथ एक चर।
पहले के लिए, मैंने इसे सीधे service.yaml में पास कर दिया है। दूसरे के लिए, मुझे कुछ मुश्किल है।
नियंत्रक में, मैं एक एपीआई से 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;
}
मैं अपनी .env फ़ाइल में JSON को स्टॉक करता हूं, MYJSON=null. यह मेरी सेवा है। yaml
# 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
App\Controller\:
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: App\Service\Error
public: true
autowire: true
app.converter:
class: App\Service\ConverterHl7Refacto
public: true
autowire: true
arguments:
$error: '@app.error'
$json: '%env(MYJSON)'
तो, मेरी सेवा में ConvertHl7Refacto.php कहा जाता है, मेरे पास कंस्ट्रक्टर में दो पैरामीटर हैं। अगर जेसन खाली या गैर है तो मैं इस्टैंस को प्रबंधित करना चाहता हूं। अगर मैं $json का dd() करता हूं, तो मुझे JSON के बजाय '%env(MYJSON)' मिलता है। क्यों?
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);
}
}
1 उत्तर
कारखाने की अवधारणा के बारे में कुछ भी विशेष रूप से मुश्किल नहीं है। एक कारखाने का उपयोग मूल रूप से किसी दिए गए प्रकार का उदाहरण बनाने के लिए किया जाता है। मैंने निम्नलिखित कोड का परीक्षण नहीं किया, इसलिए टाइपो के लिए खेद है:
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);
आपको अपने कन्वर्टर क्लास को अपनी services.yaml फ़ाइल में ऑटोवायर से बाहर करना होगा। लेकिन आपको बस इतना ही चाहिए। app.converter जैसी चीजों को स्पष्ट रूप से परिभाषित करने की आवश्यकता नहीं है क्योंकि ऑटोवायर इन सभी का ध्यान रखेगा।
संबंधित सवाल
नए सवाल
php
PHP एक व्यापक रूप से उपयोग किया जाता है, उच्च-स्तरीय, गतिशील, वस्तु-उन्मुख, और व्याख्या की गई स्क्रिप्टिंग भाषा मुख्य रूप से सर्वर-साइड वेब विकास के लिए डिज़ाइन की गई है। PHP भाषा के बारे में सवालों के लिए इस्तेमाल किया।