Postprocess request after view send

Hey,

I’m using Yii2.
After every request to an action form browser, I like to post process some things, like writing statistic data to db. But the user should not wait until those post processing-task are finish.

Is there a way in Yii2 to run some post processing steps (“in background”), after the output of an action (like rendered view or JSON) was send to the client?

In PHP world one way to do this is setting ignore_user_abort to true, but I have no idea, where to place/inject the post processing steps so that there is fired after the data was send to the user.

I don’t know whether “in background” is possible but this should be the place to try:

	'components' => [
	    'response' => [
	        'class' => 'yii\web\Response',
			'on afterSend' => function ($event) {
				Yii::debug('after send');
			},
         ]

Hey,

thanks for the idea.
Unfortunately that doesn’t work.

The following code will block the request for 10 seconds. The browser gets the page after the “on afterSend” event is done.

'components' => [
	    'response' => [
	        'class' => 'yii\web\Response',
			'on afterSend' => function ($event) {
                                sleep(10);
				Yii::debug('after send');
			},
         ]

So it looks like, the document isn’t send before this event is fired. Is this a bug or is “on afterSend” a misleading name for this event?