Laravel 5.1 using session.upload_progress

I want to create a progess panel with the status of my php script. I read that this can be done using session.upload_progress.

I am using laravel Homestead and in php.ini all requests are active.

This is my html

{!! Form::open(['route' => 'gebruikers_upload', 'class' => 'form-horizontal import', 'enctype' => 'multipart/form-data', 'target' => 'hidden_iframe']) !!} <input type="hidden" value="myForm" name="{{ini_get("session.upload_progress.name")}}"> <input type="file" name="file" id="the-file"/> <button class="btn btn-sm btn-info btn_import" type="submit">Importeer</button> <button class="btn btn-sm btn-danger" type="button">Cancel</button> {!! Form::close() !!} {{--End Form--}} <iframe id="hidden_iframe" name="hidden_iframe" src="about:blank"></iframe> <div class="progress"> <div class="progress-bar progress-bar-striped active" role="progressbar" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100" style="width: 45%"> <span class="">45% Complete</span> </div> </div> 

When sending a route:

 Route::get('dashboard/gebruikers/upload_status', ' UserController@uploadStatus '); 

And in the UserController in the uploadStatus method, I have this

 public function uploadStatus(Request $request) { session_start(); echo '<pre>'; print_r($_SESSION); echo '</pre>'; } 

But it always shows an empty array. And when I use this code

  $data = $request->session()->all(); echo '<pre>'; print_r($data); echo '</pre>'; 

He returns it

 Array ( [_token] => jFkleI9kIZJiZP3pEARx0hDrHtsynPmuGkse97nT [_previous] => Array ( [url] => http://localhost.dev:8000/dashboard/gebruikers/upload_status ) [flash] => Array ( [old] => Array ( ) [new] => Array ( ) ) [login_82e5d2c56bdd0811318f0cf078b78bfc] => 1 ) 

But there is no information on updating progress.

How can I use this with laravel 5.1

+5
source share
2 answers

I came across this when trying to solve a similar problem: updating the progress of a long-term process on the client side.

The problem is this: when you make a request to the server, the session values ​​are not written to the session store until the response is returned to the client. Basically the process is as follows:

  CLIENT | SERVER | . send request ----|----> start processing | . | . | . | finish processing | write session values receive response <----|---- return response 

What you do is something like this:

  CLIENT | SERVER | . send request ----|----> start processing | . | (update session) | . | (update session) | . | (update session) | . | finish processing | write session values receive response <----|---- return response 

But those calls (update session) on the server are ignored - they are not actually recorded in the session store. Therefore, when a client makes a call to your route to request a session value for progress, he receives nothing.

The solution is simple: write the meaning of progress elsewhere. I solved this a long time ago by writing a value to a file using file_put_contents . If I did this today, I would probably look at the redis server for better performance.

However, one note: if you decide to write the value somewhere else, you need to somehow associate the value with the session. Otherwise, other users will overwrite your value. For a simple project, I would probably use the value of the user ID (assuming that it will only process one thing at a time).

+3
source

The Laravel documentation contains all the methods needed for a communication session.

 $request->session()->put('key', 'value'); 

You can just put everything you want into a session like this.

As an auxiliary transcript, Laravel offers you the auxiliary function session() . Which take an array of "key value pairs" to set values, or just a key to retrieve any value from the session.

+1
source

Source: https://habr.com/ru/post/1234376/


All Articles