简体   繁体   中英

To Nest or Not to Nest? MVC Controllers (CodeIgniter)

Background:

I'm having some trouble understanding exactly how to best use controllers in an MVC architecture.

I have a webapp that follows this URL scheme:

  • / - main index
  • /static-page - static pages
  • /categories - index for specific categories
  • /categories/users - index for users of specific categories
  • /categories/users/id#/user-name - specific user for specific categories
  • /categories/event-type-A index for events related to specific categories
  • /categories/event-type-A/id#/event-name - specific event for specific categories
  • /categories/event-type-B index for events related to specific categories
  • /categories/event-type-B/id#/event-name - specific event for specific categories

*Note: This URL structure is similar to StackOverflow's in that malformed URL's (wrong event-name s) are automatically corrected if the id# exists.

Question:

Since categories , users , and event-type s each have unique functionality associated with them (ie editing users, manipulating events, etc.), they are all contollers of their own (right?). And since event-type-A and event-type-B are very similar, they share common functionality extended from an events class.

How do you suggest that I organize my controllers such that they follow "standard practice" in OOP and MVC design?

I currently have a Pages class for my static pages, and I was expecting to have a categories class that calls a users class, event-type-A and event-type-B classes that (as described above) are extensions of an events class... but from here I'm unsure of how best to proceed.

Any simple/pseudo code examples would be greatly appreciated.

Additional Information:

FYI: I am using PHP/MySQL. I have been trying to learn MVC by writing my own framework, but have recently switched over to CodeIgniter. That being said, either a CodeIgniter specific solution or a general MVC solution will suffice.

Update:

As Ako mentioned below , I could definitely have the events combined into a single controller and then have the two type s spawn from that. I am just confused as to how I actually set up each of the controllers (which methods to define, etc.) to make them work together properly.

It looks like this answer offers a solution that is satisfactory.

In essence:

Use HMVC to load necessary MVC triads when desired.

I had never heard of HMVC before, but apparently it will allow me to designate when certain MVC triads are called which will ultimately give me control over when different Controllers are called. This will allow each of the sections ( categories , events , types , etc.) to act as independent modules that have their own index pages that can be called, but can also offer the ability to serve much more elaborate data as needed.

How about changing /categories/event-type-A and /categories/event-type-B to /categories/events/type ? Where type could be one of A or B (or something like that)?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM