0

I have a piece of code in one of my controllers that I use to call the data for each action and subsequently each view. Rather than repeating the piece of code into each action, what is the best way to create a controller wide function in cakePHP? Or what is best practice?

Example controller:

function get_data($location) {
   $orders = $this->Post->find('all',array('conditions' => array('Post.field' => $location));
   return $orders;
}

//actual view
function index() {
   get_data(waiting);
   //etc. etc.
}

//actual view
function view_1() {
   get_data(view_1);
   //etc. etc.
}

2 Answers 2

1

The answer seems to be the fat model, skinny controller approach as outlined in this article. http://www.sanisoft.com/blog/2010/05/31/cakephp-fat-models-and-skinny-controllers/

You can declare a public function custom_function ($data) in the model and access them in the controller by $this->Model->custom_function($data);

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

1 Comment

retrieving data is for job for models! good article btw, +1 for that
0

you can use some of this actions:

beforFilter: Called before the controller action

afterFilter: Called after the controller action is run and rendered.

beforeRender: Called after the controller action is run, but before the view is rendered.

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.