skip to Main Content

There is a HTTP GET request made from angular frontend and that request is hitting our PHP controller. We are generating a reference number in controller and making some external API calls to prepare some data. I need the controller to return a response to the get request first with generated reference number and then continue doing other external API calls to prepare data.
My question is how can I achieve returning the response first from controller and then continue with other logic in same controller.
I have taken reference from here
but still controller function doesn’t actually return response until the finally block completes.

try {
    return response("", 200);
} finally {
    // Controller logic here
}

2

Answers


  1. You should probably use a Queue for that. See the Docs.

    Simple example:

    
    // Run `php artisan make:job`
    
    use AppJobsProcessDataJob;
    
    public function yourControllerMethod()
    {
        $referenceNumber = generateReferenceNumber();
    
        return response($referenceNumber, 200)
    
    
        // Dispatch the job for further processing
        ProcessDataJob::dispatch($referenceNumber);
    }
    
    Login or Signup to reply.
  2. There is also a terminable middleware. Search it in the documentation Laravel difference between terminable middleware and after middleware

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search