Mike
Mike

Reputation: 530

CakePHP: how can i handle general model functions?

I have some "global" functions not directly related to a model that need to be called from different controllers. Where can i put them and how can they be correctly called from a controller?

Upvotes: 0

Views: 370

Answers (3)

mark
mark

Reputation: 21743

it sounds like a behavior to me if the data is model related. if it is just some general method, use a component instead.

can you be more specific about the methods you want to use globally?

Upvotes: 0

Vinayak Phal
Vinayak Phal

Reputation: 8919

I would like to suggest you to create your own helper class if you want to use in views OR create your own component to use in controllers.

And group the related function in a single and give a meaningful name.

Such that you can use it in your any project just by copying them to your project.

Just add those files to your code wherever required by using the global array var $helpers = array('your_helper1','helper2'); and for components you can use var $components = array('your_component1','component2');

Upvotes: 1

Alex Ciminian
Alex Ciminian

Reputation: 11508

It depends on what your functions do. CakePHP has two generic classes: AppController and AppModel. Every controller should extend AppController and every model should extend AppModel so the methods in these to classes should be available to you in every controller.

Another alternative is to package the functions as behaviors and have all models actAs them.

Depending on semantics you may want to choose one over the other options.

Upvotes: 3

Related Questions