Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have read many posts similar to this on the site: however I still am questioning what is best in my situation (also the answers are contradicting from question to question).

I have a website (web-application) with a controller per section of domain logic (i.e. ProductsController, NewsController, ContactsController, etc..) and one very large very ugly admin controller which contains the logic for the Add/Edit/View/Delete functions for each section... now as you can imagine this controller is getting very out of control (whereas each function is not overly large, having dozens of functions in one controller does not sit well with me...).

From what I've read the general consensus is to just lump the admin functions in the 'sections' controller; however some also suggest you create a new controller for each admin section as well. so I would then have ProductsController and AdminProductsController - to me this seems a little overkill, however I have no real clue about this: any tips or readings would be appreciated - or if there is a 'industry best practise' that would be nice to know as well.

share|improve this question
add comment

4 Answers

When working with MVC-inspired patterns for the Web, it is recommended to have 1:1 relationship between Controllers and Views. This should split you code-base in much better manageable chunks.

Also, as a side effect, you might encounter situation where you are repeating same exact code with only different structures from model layer. Like for example listing latest documents and latest users. The code-flow in both controller-view pairs is the same, only difference is the source of data.

To further separate the administration from public parts, you can put relevant parts in different namespaces, which then are mapped to subdirectories with same name by the class loader.

share|improve this answer
    
+1: the right answer for the right reasons. I'll add that in my MVC framework we have a single generalized controller for the admin, streamlined to read the model and build the appropriate list or edit view. Developers can then add other controllers for more customized admin pages. –  Matt S Jul 24 '12 at 13:32
add comment

Disclaimer:My experience with MVC stems solely from Joomla!

I think separating Admin/public is exactly what you want to do. Personally, I wouldn't keep the Controllers in the same file, but rather keep all the admin components in their own subdirectory. This reduces visual complexity and keeps it simple.

share|improve this answer
add comment

This is how i lay my controllers out. Each controller is responsible for a specific group of my site.

Controllers folder

application/controllers/
  admin/
    utilities.php
    accounts.php
    invoices.php
  profile/ - any user based controllers, same as above maybe?
  auth.php - all login, password resets etc.

Views Folder

application/views
  utilities/
    partials/
      _form.php
    add.php
    edit.php
    index.php
    view.php
  accounts/
  invoices/
share|improve this answer
add comment

Why do we refactor? So that it's easier to maintain later.

What is easier to maintain?

  1. Each admin matters in the "sections" controller, polluting your probably-already-big-controllers.
  2. Each admin matters in a new controller for each section.

How you answer this question tells you what you should do.

And as @SomeKittens suggests, it does sound like you want to separate the admin part in its own folder.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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