簡體   English   中英

CakePHP 2.0,提交表單重定向無法正確呈現

[英]CakePHP 2.0, Submit form redirect not rendering properly

嗨,我已經創建了一個文件上傳表單,除了我按提交時,它都可以正常工作,它不會將我重定向到Uploads / add.ctp,但是它確實將文件保存到目錄和數據庫中。實際上,如果我將重定向指向上載/瀏覽,則仍然不需要我上載/瀏覽。

這是我的控制器

public function add() {

if(!empty($this->data)){

    $file = $this->request->data['Upload']['file'];
if ($file['error'] === UPLOAD_ERR_OK && $this->Upload->save($this->data)){
    $this->Upload->save($this->data);
    if(move_uploaded_file($file['tmp_name'],APP.'webroot/files/uploads'.DS.$this->Upload->id.'.mp4')) {
    $this->Session->setFlash(__('<p class="uploadflash">The upload has been saved</p>', true));
    $this->redirect(array('controller'=>'Uploads','action' => 'add'));
    } else{

    $this->Session->setFlash(__('<p class="uploadflash">The upload could not be saved. Please, try again.</p>', true));

    }
} 


}
}

這是我的表格

      <div class="maincontent">
      <?php echo $this->Form->create('Upload', array('type' => 'file', 'class'=>'uploadfrm'));?>
<fieldset class='registerf'>
    <legend class='registerf2'>Upload a Video</legend>
<?php
    echo 'Upload your video content here, there is no size limit however it is       <b>.mp4</b> file format only.';
    echo '<br/>';
    echo '<br/>';
    echo $this->Form->input('name', array('between'=>'<br />', 'class'=>'input'));
    echo $this->Form->input('eventname', array('between'=>'<br />'));
    echo $this->Form->input('description', array('between'=>'<br />', 'rows'=> '7', 'cols'=> '60'));
    echo  $this->Form->hidden('userid', array('id' => 'user_id','value' => $auth['id']));
    echo $this->Form->hidden('username', array('id' => 'username', 'value' => $auth['username']));
    echo $this->Form->input('file', array('type' => 'file'));
    echo "<br/>"
?>
<?php echo $this->Form->end(__('Submit', true));?>
</fieldset>

     <?php

   class UploadsController extends AppController {
   public $name = 'Uploads';

      public $helpers = array('Js');



// Users memeber area, is User logged in…
 public $components = array(
    'Session',
    'RequestHandler',
    'Auth'=>array(
        'loginRedirect'=>array('controller'=>'uploads', 'action'=>'browse'),
        'logoutRedirect'=>array('controller'=>'users', 'action'=>'login'),
        'authError'=>"Members Area Only, Please Login…",
        'authorize'=>array('Controller')
    )
  );


       public function isAuthorized($user) {
            // regular user can access the file uploads area
        if (isset($user['role']) && $user['role'] === 'regular') {
            return true;
             }

             // Default deny
                return false;
            }





function index() {
        $this->set('users', $this->Upload->find('all'));

}




// Handling File Upload Function and updating uploads database


    public function add() {

        if(!empty($this->data)){

            $file = $this->request->data['Upload']['file'];
            if ($file['error'] === UPLOAD_ERR_OK){
            $this->Upload->save($this->data); 
                if(move_uploaded_file($file['tmp_name'],APP.'webroot/files/uploads'.DS.$this->Upload->id.'.mp4')) 
                {
                    $this->redirect(array('controller' => 'Uploads', 'action' => 'add'));
                    $this->Session->setFlash(__('<p class="uploadflash">The upload has been saved</p>', true));


                    }   }else {

                        $this->Session->setFlash(__('<p class="uploadflash">The upload could not be saved. Please, try again.</p>', true));

                            }

                }
                }   

function browse () {
        // Find all in uploads database and paginates
$this->paginate = array(
    'limit' => 5    ,
    'order' => array(
        'name' => 'asc'
    )
    );
    $data = $this->paginate('Upload');
    $this->set(compact('data'));


     }

function recentuploads () {
$uploads =  $this->Upload->find('all', 
    array('limit' =>7,
    'order' => 
        array('Upload.date_uploaded' => 'desc')));
    if(isset($this->params['requested'])) { 
         return $uploads; 
         } 
            $this->set('uploads', $uploads); 
}

function watch ($id = null){
$this->set('isAjax', $this->RequestHandler->isAjax());

    // Read Uploads Table to watch video
    $this->Upload->id = $id;      
    $this->set('uploads', $this->Upload->read());



    // Load Posts Model for comments related to video
            $this->loadModel('Post');
    $this->paginate = array(
                'conditions' => array(
                'uploadid' => $id),
                'limit' => 4
                );

    $data = $this->paginate('Post');
    $this->set(compact('data'));






// Load Likes Model and retrive number of likes and dislikes

    $this->loadModel('Like');

    $related_likes = $this->Like->find('count', array(
        'conditions' => array('uploadid' => $id)
    ));
    $this->set('likes', $related_likes);
    }




}
    ?>

有什么建議么?

這個add函數在您的UploadsController中,對嗎? 您希望它重定向到上傳/瀏覽嗎?

在您的UploadsController中, $name設置為什么?

<?php
class UploadsController extends AppController {
   public $name = ?; // What is this variable set to?
}

通過Cake的Inflector,當您在重定向中指定控制器時,它應該為小寫:

$this->redirect(array('controller' => 'uploads', 'action' => 'browse'));

或者,如果您要定向的操作和您要定向的操作在同一控制器中,則甚至無需指定控制器。 例如,如果您從UploadsController add()提交表單,並且想要重定向到browser browse()

$this->redirect(array('action' => 'browse'));

嘗試一下,看看是否有幫助。

還要注意,您在add函數中兩次調用$ this-> Upload-> save($ this-> data)。

public function add() {

    if(!empty($this->data)){
        $file = $this->request->data['Upload']['file'];
        if ($file['error'] === UPLOAD_ERR_OK && $this->Upload->save($this->data)) {
            $this->Upload->save($this->data);
            if(move_uploaded_file($file['tmp_name'],APP.'webroot/files/uploads'.DS.$this->Upload->id.'.mp4')) {
                $this->Session->setFlash(__('<p class="uploadflash">The upload has been saved</p>', true));
                $this->redirect(array('controller'=>'Uploads','action' => 'add'));
            } else {
                $this->Session->setFlash(__('<p class="uploadflash">The upload could not be saved. Please, try again.</p>', true));
            }
        }
    } 

}

具體來說,在這里:

if ($file['error'] === UPLOAD_ERR_OK && $this->Upload->save($this->data)) {
    $this->Upload->save($this->data);
    ...

if條件下調用它時,它仍然將數據保存到數據庫中。 刪除第二個可以。

如果我在函數中添加以下行,請添加

            $this->render();

一切工作正常,我正在為自己的生命而苦苦掙扎,以弄清楚如果肯定要默認所有其他視圖,為什么必須渲染視圖。 但是無論如何,它都能正常工作! 希望這對其他人有幫助:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM