1

I have a function on controller like crawl.

My Controller sometimes sleeping 10 seconds, sometimes go away.

Like this:

 while ($someTrueFalse) {
        $data[]=$gettingdata;

        $wantToPassText="Received ".count($data)." - Sleeping 10 seconds";

        if($someControl){
            $someTrueFalse=false;
        }

        sleep(10);
    }

    return view('any_view');

How can I access the $wantToPassText variable from any views?

I tried,

setcookie('data',$wantToPassText,time()+60);

But i can't accessing while controller processing. I can access only when process finishes.

I tried, write database this $wantToPassText, but i can't access again while processing.

I mean, i want write on page live process status. How is it possible?

Sorry my bad english...

8
  • What do you want to achieve? Your view won't be reached unless the loop finishes processing. your view can get the $wantToPassText if it's passed from the controller to the view. Commented Mar 4, 2019 at 18:17
  • I want to show to user the processing on waiting. I can use jquery ajax methods. Forexample, i want to show to user, “received 20 datas”. 3 seconds later show “received 40 datas” when while loop returning. I tried set cookie each return of while loop, and try to read on another page with $_COOKIE but i didn’t success. Cookie setted only when controllers complete works. Commented Mar 4, 2019 at 21:16
  • put that in a job, implement socket Commented Mar 4, 2019 at 21:20
  • Could you explain? Commented Mar 4, 2019 at 21:21
  • user uploads a file and you want to process it and show user some operational updates, innit? Commented Mar 4, 2019 at 21:24

1 Answer 1

1

A solution could be to store within cache.

You can store the value like so:

Cache::put(['text' => $wantToPassText], 1);

The second argument are the amount of minutes to cache (as of Laravel 5.8 this is now the amount of seconds).

Once you have completed the task, you can then clear the cache key/value:

Cache::forget('text');

Note: You should make sure to include the facade at the top of your controller:

use Cache
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.