5
\$\begingroup\$

I'm building a CMS app with the help of a custom framework. The back-office is composed of the following pages:

  • post list
  • page list
  • user list
  • post edit
  • page edit
  • user edit

Actually, my routes are defined as follows (I don't put regex here for clarity sake):

return array(
    '/post-list/' => array(
        'controller' => 'PostListController',
        'action'     => 'view'
    ),
    '/page-list/' => array(
        'controller' => 'PageListController',
        'action'     => 'view'
    ),
    '/user-list/' => array(
        'controller' => 'UserListController',
        'action'     => 'view'
    ),
    '/post-edit/(params)' => array(
        'controller' => 'PostEditController',
        'action'     => 'view'
    ),
    '/page-edit/(params)' => array(
        'controller' => 'PageEditController',
        'action'     => 'view'
    ),
    '/user-edit/(params)' => array(
        'controller' => 'UserEditController',
        'action'     => 'view'
    ),
);

With this approach, there are many little controllers that share almost the same lines of code (not DRY code). For example:

PostListController:

public function view()
{
    $postMapper = $this->repository->getMapper('PostMapper');
    $postSlugs  = $postMapper->fetchPostSlugs();
    $posts = array();

    foreach ($postSlugs as $postSlug) {
        $posts[] = $this->repository->getModel('PostModel', $postSlug);
    }

    if ($view = $this->repository->getView('PostListView')) {
        $models = array('posts' => $posts);
        $body   = $view->render($models);

        $this->response->setBody($body);
        $this->response->send();

        return;
    }
    // ...404 handling
}

... will be nearly identical to the PageListController, the only things that change are View and Model class names.

What would be the MVC best practice or which make more sense?

  1. Keeping this approach: 1 controller for 1 page?

  2. Making a controller that handles Lists having for action: posts(), pages(), users() and a controller handling Edit the same way?

  3. Making a controller handling Post having for actions: edit() and list(), same for Page and User? (note that in this case I might still have some "unDRY" code).

  4. Something else..?

\$\endgroup\$
1
  • 1
    \$\begingroup\$ could you make a parent class or a trait to abstract out the common functionality for those controllers? \$\endgroup\$ Jan 22, 2018 at 20:42

1 Answer 1

1
\$\begingroup\$

I asked myself today again the same question for my current project where I have to implement a bunch of entities that will need their own controllers implementing usual actions such as:

  • show()
  • list()
  • edit()
  • save()
  • delete()
  • etc...

I believe we can extract from each of these actions a logic/control flow pattern common to all entities.

Thus, an abstract EntityController implementing these actions seems to me the way to go... Of course we can inject in child controllers extending EntityController specific parameters and services.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.