# Defining routes and controllers relationship for given pages

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 handle 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..?

• could you make a parent class or a trait to abstract out the common functionality for those controllers? – Sᴀᴍ Onᴇᴌᴀ Jan 22 '18 at 20:42

• show()
• list()
• edit()
• save()
• delete()
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.