Analyze. Act. Achieve.
Kurinchilion KURINCHILION Analyze. Act. Achieve.

News

cakePHP: Session enabled messages using Session->setFlash

There are different ways to flash or publish the messages for a user action. Usability plays a major role in determining how the navigation pattern for a web application takes place.

  • publishing the outcome of user action on the same page
  • designing a single page to flash all success, error, warning, notice level messages
  • designing a separate page for each message that gets published for the user

In CakePHP, to publish a message on the same page you need to make the following settings in the controller and view

In the controller //app/controllers/posts_controller.php <?php function add(){ if ($this->Post->save($this->data)) { $this->Session->setFlash('Your post has been saved.'); $this->redirect('/posts'); } } ?>

In the view

//app/views/posts/add.ctp <?php if ($session->check('Message.flash')){ $session->flash(); } echo $content_for_layout; ?>

To design a single page to flash all success, error, warning, notice level messages follow the steps below

In the controller //app/controllers/posts_controller.php <?php function add(){ if ($this->Post->save($this->data)) { $this->Session->setFlash('Your post has been saved.', 'general'); $this->redirect('/posts'); } } //end of add ?>

In the view

//app/views/layouts/general.ctp <?php echo "Message :: ".$content_for_layout; ?>

To design a separate page for each message, it is the same as the above step.But instead of one template file, you will have separate file layout file in /app/views/layout like error.ctp, warning.ctp, notice.ctp and custom design these pages to show the respective messages.