Reusing forms and controllers in Codeigniter

I am studying the OO and MVC paradigm (using Codeigniter and PHP). I keep finding warnings, such as: If you find that you are inserting the same code into multiple files, then you are not using OO / MVC correctly. So, here is a question for more experienced programmers.

  • I have a form for creating, I use two very similar versions:

    • Version 1 (at /volunteer/register) is created by an anonymous user. The form is located in the controller volunteersand must be verified by the administrator.

    • Version 2 (at /admin/create_volunteer) is created by the login administrator. The form and validation are almost the same, but are presented with different parameters.

  • Another similar example:

    I want to create various user dashboards that share a template, but will be used by different user roles and have different functions and information based on the role. As I can see, my options are:

    • Create a Dashboard controller with three functions that define the data loaded into the panel template.
    • Add a toolbar function for each role controller (Volunteer, Admin, etc.).
    • Create a controller for each case (Volunteer, Admin, etc.).

I apologize if this seems secondary, but essentially I'm looking for thumb rules to determine how to design architecture in MVC.

My questions:

  • In the first example, my logical choice of controllers ( Volunteerand Admin) less than ideal? Is code replication an acceptable practice in this case?
  • - MVC?
+3
3

, ACL (Volunteer Admin), , .

, - , , .

, , , .

+3

, ( , ), , , . , , . , , , .

, . . - . , . , - .

- , , , . , .

, , , . , ( ). , , , , .

+5

Most programmers see duplicate code as a sign that the solution can be improved.

If the problem in your case is that the from parameter is defined in one controller, but you need to use it on another controller, then you need to better define the form so that both controllers have access to it independently of each other.

Configure the form so that it can be reused.

+1
source

All Articles