Wednesday, January 30, 2013

Re: How to redirect two pages back?

Thanks, it works!.

El lunes, 16 de abril de 2012 10:19:23 UTC+2, euromark escribió:
@cricket
almost perfect - I would not pass the referer this way, though. creates overhead
and then I don't think the else case inside the post() is necessary.
after a post the same post data will populate the inputs anyway. therefore the Model.referer input should still have the same value.
no need to resend this variable to the view.
I also dont like to unnecessarily set defaults in the view.

echo $this->Form->input('Model.referer');

public function add() {

        if ($this->request->is('post')) {
                // validate ...
                
                if ($this->Model->save()) {
                        $referer = !empty($this->request->data['Model']['referer'])
                        ? $this->request->data['Model']['referer']
                        : '/';        // or some other fallback route of your choice
                        
                        $this->redirect($referer);
                }
        } else {
                $this->request->data['Model']['referer'] = $this->referer();
        }



you agree?


Am Montag, 16. April 2012 01:08:29 UTC+2 schrieb cricket:
On first display of the form pass the referer to the view to add it to the form.

echo $this->Form->input('Model.referer', array('value' => $referer));

public function add() {

        if ($this->request->is('post')) {
                
                $referer = isset($this->request->data['Model']['referer'])
                        ? $this->request->data['Model']['referer']
                        : '/';        // or some other fallback route of your choice
                        
                // validate ...
                
                if ($this->Model->save()) {
                
                        $this->redirect($referer);
                }
                else {
                        // flash msg, log, etc.
                        $this->set(compact('referer'));
                }
        }
        else {
                $this->set('referer', $this->referer());
        }
}


You have to use the else block to set the view var because if the save
failed it would overwrite it to the empty form. Likewise, you need to
create the $referer method var before attempting to save so that it's
available either way (to redirect or reset the view var).

On Sun, Apr 15, 2012 at 4:15 PM, Daniel <danw...@gmail.com> wrote:
> I am using the following code to go back a page, but the problem is
> that the action is an "add" one so it just goes back to an empty "add"
> form:
>
> if ($this->request->is('post')) {
>        // blah blah ...
>        if ($this->Inemail->save($this->request->data)) {
>                // blah blah ...
>                $this->redirect($this->referer());
>
> I think what I need to do is go back two pages.  Is this possible?
>
> Thanks.
>
> --
> Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
> Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
>
>
> To unsubscribe from this group, send email to
> cake-php+u...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments: