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?
Keeping this approach: 1 controller for 1 page?
Making a controller that handles Lists having for action:
posts()
,pages()
,users()
and a controller handling Edit the same way?Making a controller handling Post having for actions:
edit()
andlist()
, same for Page and User? (note that in this case I might still have some "unDRY" code).Something else..?