简体   繁体   English

筑巢还是不筑巢? MVC控制器(CodeIgniter)

[英]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. 我在准确地了解如何最好地使用MVC架构中的控制器时遇到了一些麻烦。

I have a webapp that follows this URL scheme: 我有一个遵循此URL方案的webapp:

  • / - main index / -主索引
  • /static-page - static pages /static-page静态页面
  • /categories - index for specific categories /categories特定类别的索引
  • /categories/users - index for users of specific categories /categories/users特定类别用户的索引
  • /categories/users/id#/user-name - specific user for specific categories /categories/users/id#/user-name -特定类别的特定用户
  • /categories/event-type-A index for events related to specific categories /categories/event-type-A type-与特定类别相关的事件/categories/event-type-A索引
  • /categories/event-type-A/id#/event-name - specific event for specific categories /categories/event-type-A/id#/event-name特定类别的特定事件
  • /categories/event-type-B index for events related to specific categories /categories/event-type-B索引,用于与特定类别相关的事件
  • /categories/event-type-B/id#/event-name - specific event for specific categories /categories/event-type-B/id#/event-name特定类别的特定事件

*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. *注意:此URL结构类似于StackOverflow的URL结构,如果存在id#则会自动更正格式错误的URL(错误的event-name )。

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?). 由于categoriesusersevent-type各自具有与之关联的独特功能(即,编辑用户,操纵事件等),因此它们都是它们自己的控制者(对吗?)。 And since event-type-A and event-type-B are very similar, they share common functionality extended from an events class. 而且由于event-type-Aevent-type-B非常相似,因此它们共享从events类扩展的通用功能。

How do you suggest that I organize my controllers such that they follow "standard practice" in OOP and MVC design? 您如何建议我组织控制器,使其遵循OOP和MVC设计中的“标准做法”?

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. 我目前有一个用于静态页面的Pages类,并且期望有一个categories类调用users类, event-type-Aevent-type-B类(如上所述),它们是events类的扩展...但是从这里我不确定如何最好地进行。

Any simple/pseudo code examples would be greatly appreciated. 任何简单/伪代码示例将不胜感激。

Additional Information: 附加信息:

FYI: I am using PHP/MySQL. 仅供参考:我正在使用PHP / MySQL。 I have been trying to learn MVC by writing my own framework, but have recently switched over to CodeIgniter. 我一直在尝试通过编写自己的框架来学习MVC,但是最近切换到了CodeIgniter。 That being said, either a CodeIgniter specific solution or a general MVC solution will suffice. 话虽如此,CodeIgniter专用解决方案或通用MVC解决方案都足够。

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. 正如下面的 Ako 所述 ,我绝对可以将events组合到一个控制器中,然后从中生成两个type s。 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. 在需要时使用HMVC加载必要的MVC三合会。

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. 我以前从未听说过HMVC ,但是显然,它可以让我指定何时调用某些MVC三合会,这些三合会最终将使我能够控制何时调用不同的Controller。 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. 这将允许每个部分( categorieseventstypes等)充当具有各自的index页(可以调用)的独立模块,但也可以根据需要提供处理更多详细数据的功能。

How about changing /categories/event-type-A and /categories/event-type-B to /categories/events/type ? 如何将/categories/event-type-A/categories/event-type-B更改为/categories/events/type Where type could be one of A or B (or something like that)? type可能是AB (或类似的东西)之一?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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