cakephp:编辑播放器后转到上一页

时间:2014-09-03 19:38:51

标签: php cakephp http-referer referer

我有网页玩家。例如,我在第13页。在这里,我点击编辑功能来编辑播放器。现在在编辑之后我想回到那个页面13,但它停留在编辑页面。

编辑操作:

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));
}

1 个答案:

答案 0 :(得分:1)

您可以将引荐页面保存在编辑功能的else结构的if/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());
  }
}