1

In Controller

public function add(){
    $this->loadModel('User'); //load model      
    if($this->request->is('post')){ 
        $filename=$this->User->checkFileUpload($this->request->data);
        $this->User->set($this->request->data); //set data to model                         
        if ($this->User->validates()){
            $datas = array(
                    'User' => array(
                                'name' => $this->request->data['User']['name'],
                                'email'=>$this->request->data['User']['email'],
                                'password'=>$this->request->data['User']['password'],
                                'image'=>$filename
                        )
                    );  
            $pathToUpload=  WWW_ROOT . 'upload/';           
            move_uploaded_file($this->request->data['User']['image']['tmp_name'],$pathToUpload.$filename);                      
            // prepare the model for adding a new entry
            $this->User->create();
            // save the data
            if($this->User->save($datas)){
                //$this->Session->setFlash('User Information has been saved!');
                return $this->Flash('User Information has been saved!',array('action' => 'index'));
                //return $this->redirect(array('action' => 'index'));
            }
        } else {
            $errors = $this->User->validationErrors; //handle errors    
        }   
    }
    //$this->layout = NULL;
    $this->viewpPath='Users';
    $this->render('add');
}

In above code, i used flash() method to direct a user to a new page after an operation. This method showing the message but not redirecting in given url. Please help me. What am i doing wrong here for redirecting with help of flash() method?

akmozo
  • 9,829
  • 3
  • 28
  • 44
Garima
  • 69
  • 2
  • 10
  • possible duplicate of [CakePHP , Controller::flash() does not redirect](http://stackoverflow.com/questions/9805952/cakephp-controllerflash-does-not-redirect) – Manoj Dhiman Jul 31 '15 at 13:01

2 Answers2

1

Render != Redirect

If you need to redirect to the referer page you can use:

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

if you want redirect to different controller:

$this->redirect(('controller' => 'YOURCONTROLLER', 'action' => 'YOURACTION'));

or if you want redirect to different action in same controller:

$this->redirect(('action' => 'YOURACTION'));
Javi
  • 506
  • 3
  • 15
  • Hi Javi, I know this is also possible from redirect() method but i want to do same work from flash() method. – Garima Jul 29 '15 at 08:52
  • if i want to show message with redirect url, firstly, i have to set setFlash() method for message then i have to use redirect() method. Instead of set message and then use redirect() method i want to do same thing with flash() method in single line. – Garima Jul 29 '15 at 09:01
1

flash() does not redirect, it renders. It is very similar to the render() function, it will continue the execution of the script, unlike the redirect() function.

but if you still want to use this

you should use following in config file.

Configure::write('debug', 0);

Update after add this into main.php use like

$this->flash(__("Some message for the user here..."), array("action" => "index"));

it'll work perfactly . Follow this forrefrence

Community
  • 1
  • 1
Manoj Dhiman
  • 5,096
  • 6
  • 29
  • 68
  • Sir, I am talking about Controller::flash(string $message, string|array $url, integer $pause, string $layout) . I read the CakePhp 2.x document and i want to use flash method just for practice but that was not working. Please check the link : http://book.cakephp.org/2.0/en/controllers.html. After open this link, please search for "Controller::flash" . I want to know how to use this method. – Garima Aug 01 '15 at 05:16
  • Thanks for reply but your updated code is working if i change debug setting from 2 to 0 in core file. – Garima Aug 03 '15 at 04:58
  • read the link http://stackoverflow.com/questions/9805952/cakephp-controllerflash-does-not-redirect. – Manoj Dhiman Aug 03 '15 at 04:59
  • this will only work if you will change debug parameter from 2 to 0 in core file – Manoj Dhiman Aug 03 '15 at 05:00
  • 1
    I finally got the answer of why i have to set 0 in debug parameter for flash method. CakePHP debug parameter says: * Production Mode: * 0: No error messages, errors, or warnings shown. Flash messages redirect. * * Development Mode: * 1: Errors and warnings shown, model caches refreshed, flash messages halted. * 2: As in 1, but also with full debug messages and SQL output. * 3: As in 2, but also with full controller dump. – Garima Aug 03 '15 at 05:36