Search code examples
phpcakephp-2.0flash-message

Flash() method shows the message but not redirecting in CakePHP 2


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?


Solution

  • 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