Cakephp redirect doesn't work with jquery mobile

564 views Asked by At

I'm trying to use CakePhp and JQuery Mobile combined. Generally it works great, but i've got a huge problem with using the Redirect from One Controller to a different.

Espacially since i added the RequestHandler.

I think the problem in this case is that, Jquery Mobile is expecting a whole page string but the controller is just giving the view back.

Is there any way to make the redirect function working, with jquery mobile?

In this case I like to redirect from Orderheads to the Orderpositions

Controller Orderheads

    if ($this->request->is ( 'post' )) {
        $this->Orderhead->create ();
        if ($this->Orderhead->saveAll ( $this->request->data,array (
                        'deep' => true 
                ))) {
            $orderId = $this->Orderhead->findByOrdernumber( $this->request->data['Orderhead']['ordernumber']);

            $id =$orderId['Orderhead']['id'];
            $this->Session->setFlash ( __ ( 'The orderhead has been saved.' ) );


            return $this->redirect ( array (
                    'controller' => 'orderpositions', 
                    'action' => 'add', $id
            ) );
        } else {
            $this->Session->setFlash ( __ ( 'The orderhead could not be saved. Please, try again.' ) );
        }
    }

Controller Orderpositions

public $components = array (
        'Paginator',
        'Session',
        'RequestHandler' 
);

public function add($id = null) {
    if ($this->request->is ( 'ajax' )) {
        if ($this->RequestHandler->isAjax ()) {
            Configure::write ( 'debug', 0 );
        }
        if (! empty ( $this->data )) {
            $this->autoRender = false;
            $this->Orderposition->create ();
            if ($this->Orderposition->save ( $this->request->data )) {
                echo 'Record has been added';
            } else {
                echo 'Error while adding record';
            }
        }
    } else {
        $this->loadModel ( 'Orderhead' );
        if ($this->Orderhead->exists ( $id )) {
            $orderInformation = $this->Orderhead->findById ( $id );
        } else {
            throw new NotFoundException ( __ ( 'Invalid order id does not exists' ) );
        }
        $this->set ( compact ( 'orderInformation' ) );
    }
}
1

There are 1 answers

0
Sigurius On BEST ANSWER

Okay I solved the problem by my self.

How I was thinking, the problem was on the JQuery Mobile Site. JQuery Mobile is normally using Ajax for linking.

This is or better said was the Problem, in combination with the cakephp flowconzept, it couldn't work. Because Jquery is always expecting the whole page(information). This is exactly why all my links with the option rel='external' worked perfect.

Btw. This behaivor is why you need the jQuery-Mobile-Subpage-Widget for using multi-pages.

But back to topic, for solving my problem with the controllers->redirect function, I just had to add the Data-Ajax='false' option to the options parameter of the cakephp Form-Create function.

If you set that parameter the link will set a full-page-request instead an ajax-request.

example:

<?php
    echo $this->Form->create('Contactperson', array(
                            'data-ajax' => 'false'));

    echo $this->Form->input('name');
    echo $this->Form->input('surname');
    echo $this->Form->input('email');

    echo $this->Form->end(__('Submit'));
?>

I hope this can help any other people with the same problem, I've wastet a fucking lot time with that stuff.