如何在上传文件后将用户重定向到视图页面?
现在它只在我在视图中写入db条目的id
时才有效我是php和yii 2的新手。
控制器
public function actionUpload()
{
$model = new UploadForm();
if (Yii::$app->request->isPost) {
$model->file = UploadedFile::getInstance($model, 'file');
if ($model->file && $model->validate()) {
$postFile = Yii::$app->request->post();
$directory = Yii::getAlias('@frontend/web/uploads');
$uid = Yii::$app->security->generateRandomString(6);
$fileName = $uid . '.' . $model->file->extension;
$filePath = $directory . '/' . $fileName;
$path = 'http://frontend.dev/uploads/' . $fileName;
$userinfo = new webminfo();
$userinfo->uid = $uid;
$userinfo->author_id = Yii::$app->user->id;
$userinfo->path = $filePath;
$userinfo->url = $path;
$userinfo->created_at = time();
$userinfo->ip = Yii::$app->getRequest()->getUserIP();
$userinfo->save();
if ($model->file->saveAs($filePath)) {
Yii::$app->session->setFlash('success', 'Success');
return $this->redirect(['view', 'id' => $userinfo->uid]);
}
}
else {
Yii::$app->session->setFlash('error', 'Error');
}
}
return $this->render('upload', ['model' => $model]);
}
public function actionView($id) {
$model = new webminfo();
return $this->render('view', [
'id' => $id,
'model' => $model,
]);
}
查看
<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use yii\helpers\Url;
use app\models\webminfo;
$this->title = 'View';
$this->params['breadcrumbs'][] = $this->title;
?>
答案 0 :(得分:0)
在重定向功能
中使用$uid
代替$userinfo->uid
,如下所示
return $this->redirect(['view', 'id' => $uid]);