So I have an upload form. It uploads an image, but I want it to redirect to a success view with some additional information.
I had a function in my FileController called actionSuccess, but I don’t really know what to put in this function in order to check if you came from the file upload page with an upload that was valid. This page should also be unavailable to view without uploading anything.
I’m not even sure whether or not I should have the function actionSuccess.
class SomeController {
public function actionSomeUploadHandler() {
// HANDLE YOUR UPLOAD HERE THEN IN IF
if($upload_success) {
Yii::app()->getUser()->setFlash('upload-done', TRUE);
$this->redirect('success');
} else {
// DO FOR FAILED UPLOAD
// OR
// SET FLASH AGAIN TO HANDLE/RENDER ANOTHER VIEW
}
}
public function actionSuccess() {
if(Yii::app()->request->urlReferrer === $this->createUrl('SomeUploadHandler') && Yii::app()->user->hasFlash('upload-done'))
$this->render('RENDER-SUCCESS-VIEW-HERE');
// <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/smile.gif' class='bbc_emoticon' alt=':)' />
} else {
$this->render('CAN-RENDER-FAILED-VIEW-TOO');
// <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/sad.gif' class='bbc_emoticon' alt=':(' />
}
}
}
if (Yii::app()->request->urlReferrer === $this->createUrl('SomeUploadHandler'))
will return false, because the way it outputs both the variables is different.
Yii::app()->request->urlReferrer
will get the URL which you came from, eg: www.example.com/index.php/SomeUploadHandler/1, while the createUrl function from the controller will output something similar to /SomeUploadHandler/1.
I solved this by simply using PHP’s substr function.
This way isn’t 100 % reusable, sadly, but it works for my one-man project and I’ll change if it’s needed to maybe a regex function or similar.