My project uses Zend Framework 2.
I'm trying to determine if I am doing too much in my controller as a rule for my development. I have built my controllers like this for a while and haven't run into many problems but I wanted to get some feedback from this community.
This specific action handles a video management form that contains an image upload form. Is the controller for editing information about videos doing too much?
Here is the action from my controller:
public function editAction() {
$id = $this->params('id',null);
$sl = $this->getServiceLocator();
$form = $sl->get('AdminVideoForm');
$image_form = $sl->get('VideoImageUploadForm');
$image_form->setAttribute('action',$this->url()->fromRoute('admin/video/upload-image') . '?video_id=' . $id);
$model = [
'form' => $form,
'image_form' => $image_form
];
$current_video = $this->videoTable->fetchById($id);
if(!$current_video) {
$this->flashMessenger()->setNamespace('video-admin')->addMessage('Invalid Video ID');
return $this->redirectToListing();
}
$model['id'] = $id;
$model['video'] = $current_video;
$form->bind($current_video);
$request = $this->getRequest();
if($request->getQuery('returnTo')) {
$model['returnTo'] = $request->getQuery('returnTo');
}
else {
$model['returnTo'] = $this->url()->fromRoute('admin/video');
}
$form->setAttribute('action',$this->url()->fromRoute('admin/video/edit',['id' => $id]) . '?returnTo=' . rawurlencode($model['returnTo']));
if($request->isPost()) {
$form->setData($request->getPost());
if($form->isValid()) {
$this->videoTable->saveVideo($form->getData());
return $this->redirectToListing();
}
}
return $model;
}
1 Answer 1
The method is 37 lines total, which does seem somewhat long yet is not uncommon in many projects. One could move things around a bit to avoid assigning variables like $model
, $image_form
, etc. when they are not used (e.g. in early returns):
- the early return when the
$current_video
could possibly be moved closer to the assignment of$id
- the early return when
$request->isPost()
istrue
-thy and the form is valid could be moved up
The setting of $model['returnTo']
could be simplified with a ternary operator:
$returnTo = $request->getQuery('returnTo');
$model['returnTo'] = $returnTo ? $returnTo : $this->url()->fromRoute('admin/video');
The ternary short cut ?:
, A.K.A. "the Elvis operator" has been available since PHP 5.3 and can be used to simplify this:
$model['returnTo'] = $request->getQuery('returnTo') ?: $this->url()->fromRoute('admin/video');
Elvis face superimposed with characters
The code below has not been tested but shows an example of how it could be simplified somewhat. I made a few modifications per the PSR-12 style guide - e.g. adding a space after a comma when separating function/method parameters, limiting line length to 120 characters (which is why $urlFromRoute
is declared), etc. One could possibly abstract the last 11 lines to a separate method to get the model array.
public function editAction() {
$id = $this->params('id', null);
$current_video = $this->videoTable->fetchById($id);
if (!$current_video) {
$this->flashMessenger()->setNamespace('video-admin')->addMessage('Invalid Video ID');
return $this->redirectToListing();
}
$request = $this->getRequest();
$sl = $this->getServiceLocator();
$form = $sl->get('AdminVideoForm');
$form->bind($current_video);
if ($request->isPost()) {
$form->setData($request->getPost());
if ($form->isValid()) {
$this->videoTable->saveVideo($form->getData());
return $this->redirectToListing();
}
}
$image_form = $sl->get('VideoImageUploadForm');
$image_form->setAttribute('action', $this->url()->fromRoute('admin/video/upload-image') . '?video_id=' . $id);
$returnTo = $request->getQuery('returnTo') ?: $this->url()->fromRoute('admin/video');
$urlFromRoute = $this->url()->fromRoute('admin/video/edit', ['id' => $id]);
$form->setAttribute('action', $urlFromRoute . '?returnTo=' . rawurlencode($returnTo));
return [
'video' => $current_video,
'id' => $id,
'form' => $form,
'image_form' => $image_form,
'returnTo' => $returnTo
];
}