变量可用于服务

问题描述:

我是一个初学者在PHP。我有一个WadoService服务和一个StudiesRestController控制器。我想在服务中使用控制器数据。变量可用于服务

public function getPatientAction(Request $request, $studyUID) 
{ 
    $studyRepository = new StudyRepository(
     $this->get('nexus_db'), 
     $this->get('logger'), 
     $this->get('translator') 
    ); 

    $study = $studyRepository->getStudy($studyUID); 
    if (!$study) { 
     throw new NotFoundHttpException("No study found with studyuid $studyUID"); 
    } 

    $patientInfo = new RestResponse(
     SerializerBuilder::create() 
      ->build() 
      ->serialize($study->getPatient(), 'json') 
    ); 

    return $patientInfo; 
} 

这可能吗?我试图把这个功能getPatientAction()无果而终:

/* @var $wadoService WadoService */ 
    $wadoService = $this->container->get(WadoService::SERVICE_NAME); 

    $wadoService = new RestResponse(
     SerializerBuilder::create() 
      ->build() 
      ->serialize($study->getPatient(), 'json') 
    ); 
+0

您使用哪种技术? Drupal 8? Symfony? Laravel? – teeyo

+0

目前还不清楚你在问什么。您发布的代码示例与该问题无关。 – SirDarius

+0

@teeyo 我使用Symfony。 – Floriane

要从控制器变量传递给你的服务,你这样做会这样:

$wadoService = $this->container->get(WadoService::SERVICE_NAME)->yourServiceMethod($yourVari); 
+0

我想在我的服务中使用变量'$ patientInfo'。我需要这样做吗? '$ wadoService = $ this-> container-> get(WadoService :: SERVICE_NAME); $ wadoService-> getPatientInfo($ patientInfo);' – Floriane

+0

是的!你能把答案解决好吗? –