I'm learning the OO and MVC paradigm (using Codeigniter and PHP). I continue to find warnings such as this: "If you find yourself pasting the same code into multiple files, then you aren't using OO/MVC properly." So, here's a question for more experienced programmers:

I have a create-user form that I am using two very similar versions of:

Version 1 (at /volunteer/register) is created by an anonymous user. The form lives in the volunteers controller, and needs to be verified by an admin.

Version 2 (at /admin/create_volunteer) is created by a logged-in admin. The form and validation is nearly the same, but it is submitted with different parameters.

Another, similar example:

I want to build different user dashboards that share a template, but will be used by different user roles and have different functions and information based on role. As I see it my choices are:

  1. Create a Dashboard controller with three functions defining the data loaded into the dashboard template.
  2. Add the a dashboard function to each role's controller (Volunteer, Admin etc).
  3. Create a controller for each case (Volunteer, Admin, etc.)

I apologize if this appears sophomoric but essentially I'm looking for rules-of-thumb to determine how to design architecture in MVC.

My questions:

• In the first example, is my logical choice of controllers (Volunteer & Admin) less than ideal? Is code replication in this case an acceptable practice? • Can anyone recommend architecting tools to establish logical consistency and good workflow for MVC?

Accepted Answer

It seems you would like to use some ACL for distinction between roles (Volunteer and Admin) that will check whether requested module or action can be accessed.

Creating different controllers for the roles doesn't seem to be a good choice since this architecture could not be reused - you don't want to reuse specific Admin or Volunteer functions in other applications but rather a module that lets you create and control such roles.

You would like to reuse a code offering particular functionality, this might be one controller, one model and some view files.

Written by deczo
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki