简体   繁体   中英

Cakephp redirection after submission

I'm developing an application with Cakephp and I can't find my answer in existing topics...

I have a main menu with several action (Edit/Delete/Add) which is manage sockets. When I edit a socket and I submit my update, my socket is updated correctly BUT my redirection on the main menu doesn't work. The URL stay the same. For example, I edit socket with id = 2 . The application go to '/my/app/sockets/edit/2' . After update and submit, the application should redirect to '/my/app/sockets/index' but it doesn't work.

Below, you can see my code. 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>

I've already search and it could be a cache problem. Thanks in advance.

Can you force with

$this->redirect($this->referer());

Please, try and comment

Your request checking if-statement is omitted, because is() method accepts param of string type according to documentation .

So you should change line:

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

into:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM