views:

329

answers:

1

This site is built using CakePHP 1.2*

I have an element that needs to be placed in multiple views under multiple controllers. This element is very dynamic, every time a page is loaded it needs to call a function (which is rather large) and then display what was returned.

There are a few options that I have thought of, none of which I am sure are the correct way of doing this.

  • Placing this function within app_controller and then somehow calling it from the element
  • create a helper and then including it in every controller that will display the element.
  • creating a component and then including it in every controller that will display the element.

Any help is appreciated in the correct way of doing this. Currently the element is being placed on multiple pages under multiple controllers and is calling the function using $this->requestAction() which from what I understand is certainly not the correct way.

Thanks in advance!

+1  A: 

Well, it's a tricky question, maybe you should give a little more of information. But I'll try:

  • If your function is part of your app's logic, you should make it a component.
  • If this function is part of the view, you should make it a helper.
  • If this function must be executed in your view, you could also try $this->render();

It's a tricky question, I hope this could help you, but you should give a little extra info.

metrobalderas
Thank you very much for the insight, sorry I am not sure how I could be any more specific however because it displays the results of the function, it should be a helper. Thank you!
metro - at what point do you generally split out helpers and elements? For me, it's when I try to break an element into different methods, but I'm curious about how other people do it. Do you ever pass arguments to an element, or would that scream "helper" to you? And finally, would you ever echo directly from the helper, or just return the output, to have the invoking element echo it?
Travis Leleu
I'm no cakePHP guru, but I use elements whenever I want to recycle certain format consistent with the design. That could be the up and down triangles in this site, used both in questions and answers. The helper is cool as long as you don't spit out too much html (think a Number helper so you can split by a comma the thousands and millions).One simple rule: if it involves too much html and you want to recycle, go with an element. If not, a helper is fine. My two cents.
metrobalderas