Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • CakePHP: Call to a member function setFlash() on a non-object

    • 0
    • 1
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 1.25k
    Comment on it

    Hello Readers,

    We have a session helper in cakephp which is known as setFlash() basically use for showing messages after login successfully , user registration etc.

    First of all we have to assign the session helper in Controller.

    Code use in Controller:

    public $Helpers = array('Session','Html');
    public function contact us(){
    
            if($this->request->data){
    
                if($this->Contact->save($this->request->data)){
                    $this->Session->setFlash("*Thanks For Contacting Us! We will get you Soon."); 
                    $this->redirect(array('controller'=>'users','action'=>'contact us'));
                }
                else{
                    $this->Session->setFlash("Data Not Saved",'default');
                    $this->redirect(array('controller'=>'users','action'=>'contact us'));
                }
            }
        }
    

    above code explains that when contact details successfully saved in database then show the Thank you message otherwise show the error message.


    Code use in Contact us ctp

    <html>
    <?php echo $this->Session->flash();?>
    
    //
      write html code here
    //
    </html>
    

    In above code <?php echo $this->Session->flash();?> simply echo your message which you provided in controller.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: