0

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?

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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');

Comments

0

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.