I just started creating a simple web application with cakePHP and was wondering if I could avoid code duplication in my actions. I have got two models and the corresponding controllers both contain the same actions (index, view, add, edit, delete) with marginally different code, e.g.:
Transaction controller
public function add() {
if ($this->request->is('post')) {
$this->Transaction->create();
if ($this->Transaction->save($this->request->data)) {
$this->Session->setFlash(__('The transaction has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The transaction could not be saved. Please, try again.'));
}
}
}
The second controller would have the same add() action, only for a different model (i.e. replace transaction by e.g. trades).
So is there a way to avoid this kind of code duplication?