I want to refresh my page to update view of data after downloading a file, and if i use a variable to process download then back to referrer it doesn't work. I'm confused because i can't use return
twice, how to solve it?
my code in controller:
if (file_exists($root)) {
// here's the problem:
return Yii::$app->response->sendFile($root, $profile['resumefilename']); //download resume
return $this->redirect(Yii::$app->request->referrer); // i can't put return here, but i need to refresh page after download
} else {
Yii::$app->session->setFlash('failed', 'Data resume is not found!');
return $this->redirect(Yii::$app->request->referrer);
}
My Answer:
In Yii2, you can use the `Yii::$app->request->referrer` to get the referrer URL or `Yii::$app->request->isAjax` to check if the request is an AJAX request.
Here is an example of how you can return the referrer URL or refresh the page after processing a download in Yii2:
php
public function actionDownload()
{
// Process download here
if(Yii::$app->request->referrer) {
return $this->redirect(Yii::$app->request->referrer);
} else {
return $this->refresh();
}
}
In this example, the `actionDownload` method processes the download and then checks if there is a referrer URL. If there is a referrer URL, it redirects the user back to that URL. If there is no referrer URL, it refreshes the current page.
You can customize this logic based on your specific requirements.
Rate this post
4 of 5 based on 4747 votesComments