Controllers and re-factoring for maintainability / sensibility

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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.
</code>
<code>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. </code>
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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>application/views
utilities/
partials/
_form.php
add.php
edit.php
index.php
view.php
accounts/
invoices/
</code>
<code>application/views utilities/ partials/ _form.php add.php edit.php index.php view.php accounts/ invoices/ </code>
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?

  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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật