We are using CodeIgniter to wrap some internal functions and to display the results in json.
Occasionally our internal functions might have debug strings printed to the screen. So we have started using output buffering to capture any debug and add this to the result $data['response']['debug'] = $ob;
This seems to have worked pretty well as we have started dropping this code into most our publicly exposed CI functions.
We seem to be repeating this code flow a-lot now.
What would be the best way to extract the 'repeated' sections into a general template that is called when required? (not every function implements this - just most).
Example code:
public function some_function($var){
ob_start(); //repeated
$this->load->model('some_model');
$result = $this->some_model->do_something($var);
if($result){
$data['response']['result'] = true;
}else{
$data['response']['error'] = 'Something not found.';
}
$ob = ob_get_clean(); //repeated
if($ob!=''){
$data['response']['debug'] = $ob; //repeated
}
$this->load->view('json',$data); //repeated
}