So I'm really trying to figure it out how can I do that in Symfony 5.
I have an services named PaymentRequestService which have the entire logic for requests to another application (based on microservices).
I injected PaymentRequestService in PaymentService as constructor, an service which processes data, make validation, etc.
And right now I'm trying to call from my controller an method from PaymentRequestService by using PaymentService. Something like that: $paymentService->$paymentRequestService->method.
Can someone tell me how can I do that?
Right now it looks something like that $payment = $paymentRequestService->getPaymentRequest((string)$id)
But I want to eliminate PaymentRequestService.
My Answer:
> You can call a method from PaymentRequestService within PaymentService by simply creating a public method in PaymentService that calls the method from PaymentRequestService. Here's an example:
php
// PaymentService.php
use App\Service\PaymentRequestService;
class PaymentService
{
private $paymentRequestService;
public function __construct(PaymentRequestService $paymentRequestService)
{
$this->paymentRequestService = $paymentRequestService;
}
public function callPaymentRequestServiceMethod()
{
// Call the method from PaymentRequestService
$this->paymentRequestService->methodName();
}
}
Then, in your controller, you can call the `callPaymentRequestServiceMethod()` method from PaymentService:
php
// YourController.php
use App\Service\PaymentService;
class YourController extends AbstractController
{
private $paymentService;
public function __construct(PaymentService $paymentService)
{
$this->paymentService = $paymentService;
}
public function yourAction()
{
// Call the method from PaymentService which in turn calls the method from PaymentRequestService
$this->paymentService->callPaymentRequestServiceMethod();
}
}
This way, you can access the methods from PaymentRequestService through PaymentService in your controller.
Rate this post
3 of 5 based on 4245 votesComments