I want to set flash message, but flash message missing if i add redirect. if i remove the line of redirect code then the flash message will be shown, but if i remove the redirect when i click on F5 refresh it will run again the file submission. How to make redirect work together with the flash message?
the flow of my controller like this:
public function actionImportVehiclecsv()
{
$model = new CrmImportVehicle();
$session = Yii::$app->session; // Get the session component
if (Yii::$app->request->isPost) {
$model->csvVehicleFile = UploadedFile::getInstance($model, 'csvVehicleFile');
$model->csvAccount = Yii::$app->request->post('CrmImportVehicle')['csvAccount'];
if ($model->csvVehicleFile) {
if ($filePath = $model->upload()) {
$processResult = $model->processVehicleFile($filePath);
if ($processResult === true) {
$session->setFlash('success', 'CSV file uploaded and data inserted successfully.');
} elseif ($processResult === false) {
$session->setFlash('error', 'Error in CSV row: Vehicle No or Username cannot be blank');
} else {
$session->setFlash('error', $processResult); // Set error flash message for other errors
}
$session->set('processed', true); // Indicate that the form was processed
} else {
$session->setFlash('error', 'Error uploading CSV file.');
}
} else {
$session->setFlash('error', 'No file was uploaded.');
}
return $this->refresh(); // if i remove this line, the flash message works.
} elseif ($session->get('processed')) {
$session->remove('processed'); // Clear the 'processed' flag for GET requests
return $this->refresh(); // Refresh the page if it was previously processed
}
// Render the view for GET requests
return $this->render('import-vehiclecsv', ['model' => $model]);
}


The concept of flash messages is in the principle that the message exists only for one request and are then deleted.
I would advise you to check whether you can retain the messages even after a refresh by using the
removeAfterAccessparameter.Source: Yii documentation