sdot257
sdot257

Reputation: 10366

PHP flush() doesn't work with laravel?

I'm using Laravel and I need to output data as it happens. When I attempt to load the page outside Laravel, it works just fine. If I use this inside Laravel, it doesn't flush, it waits until the end and prints the results.

view.php

<?php

if (ob_get_level() == 0) ob_start();
for ($i = 0; $i <= 10; $i++){

    echo "<br> Line to show. $i";
    echo str_pad('',4096)."\n";    

    ob_flush();
    flush();
    sleep(1);

}
ob_end_flush();
?>

Upvotes: 6

Views: 6537

Answers (3)

Yosef
Yosef

Reputation: 11

Update, for anyone coming here.

The above solutions didn't work for me. What worked, is adding the

header('X-Accel-Buffering: no');

before any output.

Found it here: https://laracasts.com/discuss/channels/laravel/live-output-in-blade-template-ob-flush

Upvotes: 1

Deepak Thomas
Deepak Thomas

Reputation: 3515

This sequence worked for me.

ob_implicit_flush(true);
echo "Processing ... "; // Or give out JSON output
ob_flush();
sleep(5); //A time-consuming synchronous process (SMTP mail, maybe?)
echo "Done";

Upvotes: 0

sdot257
sdot257

Reputation: 10366

Figured it out, I needed to add ob_flush();

Upvotes: 6

Related Questions