2014-09-03 14 views
0

我有玩家在頁面中。我例如在第13頁。在這裏我點擊編輯功能來編輯播放器。現在編輯後,我想回到那個頁面13,但是它停留在編輯頁面。cakephp:編輯播放器後轉到上一頁

編輯操作:

public function admin_edit($id = null) { 
    if (!$this->Player->exists($id)) { 
     throw new NotFoundException(__('Invalid player')); 
    } 
    if ($this->request->is(array('post', 'put'))) { 
     $data = $this->request->data['Player']; 
     if(!$data['player_image']['name']){ 
      unset($data['player_image']); 
     } 
     if ($this->Player->save($data)) { 
      $this->Session->setFlash(__('The player has been saved.')); 
      $this->redirect($this->referer()); 
     } else { 
      $this->Session->setFlash(__('The player could not be saved. Please, try again.')); 
     } 
    } else { 
     $options = array('conditions' => array('Player.' . $this->Player->primaryKey => $id)); 
     $this->request->data = $this->Player->find('first', $options); 
    } 
    $videos = $this->Player->Video->find('list'); 
    $this->set(compact('videos')); 
} 

觀點行動:

public function admin_view($id = null) { 
    if (!$this->Player->exists($id)) { 
     throw new NotFoundException(__('Invalid player')); 
    } 
    $options = array('conditions' => array('Player.' . $this->Player->primaryKey => $id)); 
    $this->set('player', $this->Player->find('first', $options)); 
} 
+0

你不需要返回重定向,你有沒有嘗試過'$這個 - >重定向($ this-> referer())'?或者調試'$ this-> referer',可能它來自同一個編輯頁面,這就是爲什麼它停留在那裏的原因(我無法從你提供的代碼中知道) – Nunser 2014-09-03 20:09:01

+0

你給出的兩個選項給了我相同的結果:/ – Gilko 2014-09-03 20:36:24

+0

你告訴我,如果你在'admin_edit'中調試'$ this-> referer()',你會得到'admin/controller/view/13'嗎?懷疑,除非保存,如果失敗。 – Nunser 2014-09-03 20:46:19

回答

1

您可以在編輯功能的if/else結構的else部分保存引用頁。然後在if(即$this->request->is(array('post', 'put')) = TRUE部分使用儲值

所以,你的代碼看起來是這樣的:

public function admin_edit($id = null) { 
    if ($this->request->is(array('post', 'put'))) { 

    /* your other code */ 

    $sendback = $this->Session->read('referer'); 
    $this->Session->delete('referer'); 
    $this->redirect($sendback); 

    } else { 

    /* your other code */ 

    $this->Session->write('referer', $this->referer()); 
    } 
} 
相關問題