views:

25

answers:

1

I am trying to create a message-board type element in a CakePHP app. This element will be displayed on all pages and views that use a particular layout. I want it to display all the messages in the model, then show the add form when a link is clicked, then return to the updated message list when submitted. All this without affecting the current view/page.

I have my message model/controller/index set up, with a message board element that requests the index action. This works fine. However I am perplexed about how to return back to the original page/action from which the link was clicked. I can't use $this->referer() because that will link back to the add() action; what I want rather is to link to the page/view before that.

Any general pointers on how to achieve something like this?

A: 

I would approach this using Ajax, and use an ajax layout.

$this->layout('ajax')

Then you would be able to setup a full stack for processing this, and pass various things in as parameters into the controller actions.

By using Ajax you will not need to worry about passing in the referrer controller / action pair. You can also use the return from this to update the list by calling out to the MessagesController. The added bonus of this is that you can just switch the layout in your actual controllers, thus not having to write any extra code at all.

In your controller, you can check for Ajax

if($this->params['requested']){
  $this->layout('ajax');
  return $data;
}else{
  $this->set('data',$data);
}
DavidYell
Thanks for the direction. I will look into this.
Logic Artist