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.
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.
1
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.
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/
Why do we refactor? So that it’s easier to maintain later.
What is easier to maintain?
- Each admin matters in the “sections” controller, polluting your probably-already-big-controllers.
- 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.