When I have a controller, e.g. article I often have a action_view()
that handles most of the code.
Sometimes, it can become 80-100 lines long.
My controller usually handles all of these:
- binding template variables
- setting sessions (where appropriate)
- sending emails
- validating forms
I could see bit and pieces that I could make another private method in the controller, not necessarily for reuse, but for separation of concerns.
However, then it looks weird (to me) having methods which can be called via a route, and methods that are internal only.
Also some things say to me "I should be in the model, not the controller". However, I'm not sure if that's correct either.
In the end, I have a somewhat fat controller method that looks quite procedural.
Should I actually have a list up top of my action_*
methods, and then separate the rest of my code into smaller modules?
I've got an example below... is this typical controller stuff, or should the sessions etc be in the model?
public function action_pdf($type, $id) {
// Get PDF file from db and send headers to it
$id = (int) $id;
$pdfFile = $this->model->getPdf($id, $type);
if ($pdfFile) {
$this->request->redirect($pdfFile);
} else {
$this->session->set('pdfMissing', true);
$this->request->redirect(Route::get('properties')->uri());
}
}
So, my question is, am I doing it wrong?