M
M
MasterCopipaster2020-09-24 01:50:55
Laravel
MasterCopipaster, 2020-09-24 01:50:55

Laravel how to send code 102 in controller and then 200?

What should I write in the controller to repeat the simple example below?

<?php
header('HTTP/1.1 102 Processing'); // let client know it might take a while
sleep(2); // do stuff that takes a while
header('HTTP/1.1 200 OK'); // counterintuitive, but works

Own controller
public function destroy($id)
    {
        try {
            $auto_mark = Mark::findOrFail($id);
        } catch (\Exception $e) {
            return $this->failedRequest('NOT_FOUND');
        }
        try {
            $auto_mark->delete();
        } catch (\Exception $e) {
            return $this->failedRequest('DELETE_FAIL');
        }
        return response()->json(['status' => 'success'], 200);
    }


I do not quite understand how I can do what would be before $auto_mark->delete(); laravel responded to browser 102 and continued? after all, if I return the response code through return, then the controller will stop at this point?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2020-10-27
@dlnsk

Are you sure that it is in this context should be used?
It seems to me that the sequence here is something like this:
1. A request for a lengthy operation is sent from SPA using, for example, ajax.
2. At certain intervals, a request is sent via ajax about the status of the task. The server responds with 102 if the task is still running, and in the body, for example, the percentage of completion.
3. At the next status request, the server responds with 200, indicating the operation performed. SPA changes the interface accordingly.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question