0

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' ) );
    }
}
Sigurius
  • 41
  • 1
  • 5
  • if ($this->request->is ( 'ajax' )) and ($this->RequestHandler->isAjax ()) do the same thing.. avoid the second perhaps. – MouradK Dec 15 '14 at 11:27
  • Thx, but this cant resolve my problem :( . I'm really sure the redirect doesn't work, because jquery mobile is expecting an whole page but gets just the VIEW information from the Controller->redirect ... Isn't there any way to say cakephp that it has to send the whole page? I tried a lot of different things but nothing worked. If I work with links (rel=external), the redirections aren't a problem...just the $this->redirect function from inside the controller doesn't work :( – Sigurius Dec 15 '14 at 16:03
  • Okay I solved the problem now. It's unbeliveable how easy it was O_o Just enter Data-Ajax='false' into to Form->create Options. Form->create('COrderheads', array( 'data-ajax' => 'false')); ?> – Sigurius Dec 15 '14 at 20:29

1 Answers1

1

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.

Sigurius
  • 41
  • 1
  • 5