提交后的Cakephp重定向

时间:2015-07-16 08:09:14

标签: php cakephp redirect caching url-redirection

我正在使用Cakephp开发一个应用程序,但我无法在现有主题中找到答案...

我有一个带有多个操作(编辑/删除/添加)的主菜单,它是管理套接字。当我编辑套接字并提交更新时,我的套接字正确更新但是主菜单上的重定向不起作用。 URL保持不变。 例如,我使用 id = 2 编辑套接字。该应用程序转到' / my / app / sockets / edit / 2' 。更新并提交后,应用程序应重定向到' / my / app / sockets / index' ,但它不起作用。

下面,您可以看到我的代码。 SocketsController.php:

public function index($searchCloset = null) {

    $this->Paginator->settings = $this->paginate;
    if($searchCloset != null) {
        $sockets = $this->paginate('Socket', array(
            'Closet.name LIKE' => $searchCloset
        ));
    }
    else{
        $sockets = $this->paginate('Socket');
        $searchCloset = '' ;
    } 

    $this->set('sockets',$sockets);
    $this->set('closets',$this->Socket->Closet->find('all'));
    $this->set('searchCloset',$searchCloset);
}
public function edit($id = null){
    // Bad socket management
    if (!$id) {
        throw new NotFoundException(__('Invalid socket'));
    }

    $socket = $this->Socket->findByid_socket($id);
    if (!$socket) {
        throw new NotFoundException(__('Invalid socket'));
    }

    // if there's a submit
    if ($this->request->is(array('post'))) {
        $this->Socket->id = $id;
        if ($this->Socket->save($this->request->data)) {
            // update 'lastchange' column
            if($this->updateSocketStatus($this->Socket->id)){
                $this->Session->setFlash(__('Your socket has been updated.'));
            }
            else {
                $this->Session->setFlash(__('Unable to create history.'));
            }
        }
        else {
            $this->Session->setFlash(__('Unable to update your socket.'));
        }

        return $this->redirect(array('controller' => 'sockets','action' => 'index'));
    }    

    // send all informations about the socket to the view
    $this->set('socket',$socket);
    // send closets list to the view (select menu)
    $this->set('closets',$this->Socket->Closet->find('list',array(
        'fields' => array('Closet.id_closet','Closet.name')
    )));
}

edit.ctp:

<h2>Edit socket</h2>
<div><?php 
echo $this->Form->create('Socket');
echo $this->Form->input('Socket.name', array(
    'label' => 'Socket name :',
    'default' => $socket['Socket']['name']
    ));
echo $this->Form->input('Socket.location', array(
    'label' => 'Location :',
    'default' => $socket['Socket']['location']
    ));
echo $this->Form->input('Socket.comment', array(
    'label' => 'Comment :',
    'default' => $socket['Socket']['comment']
    ));
echo $this->Form->input('Socket.closet_id',array(
    'type' => 'select',
    'label' => 'Closet :',
    'options' => $closets,
    'default' => $socket['Closet']['id_closet']
));
echo $this->Form->end('Save socket');
echo $this->Form->postButton(
        'Back',
        array('controller' => 'sockets','action' => 'index'),
        array(
            'id' => 'back_button',
            'class' => 'ui-btn ui-btn-inline ui-icon-back ui-btn-icon-left'
        )
);
?></div>

我已经搜索过,这可能是一个缓存问题。 提前谢谢。

2 个答案:

答案 0 :(得分:1)

你能用

强行吗?
$this->redirect($this->referer());

请尝试发表评论

答案 1 :(得分:0)

您的请求检查if-statement是否被省略,因为is()方法根据documentation接受字符串类型的参数。

所以你应该换行:

if ($this->request->is(array('post')))  { ... }

成:

if ($this->request->is('post')) { ... }