2017-12-29 17 views
0

파일을 업로드 한 후 사용자를보기 페이지로 리디렉션 할 수 있습니까? 내가보기에 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

당신은'$의 id'을 통과하지 않으면 당신은'actionView ($ 아이디)'pravindot17 @ – pravindot17

+0

내가 그것을 어떻게 전달할 수있는 필수 PARAM을 가지고있는보기가 오류가 발생합니다? –

+0

'return $ this-> redirect ([ 'view', 'id'=> $ userinfo-> uid]); 이것은 올바른 방법이지만'$ userinfo-> uid'에서 어떤 값을 얻었는지 확인 했습니까? , 그렇지 않으면'return $ this-> redirect ([ 'view', 'id'=> $ uid]); '를 시도해보십시오 – pravindot17

답변

0

리디렉션에서 다음과 같이 $userinfo->uid 대신 $uid을 사용하십시오. 기능

return $this->redirect(['view', 'id' => $uid]);