用于MVC架构的多级控制器 [英] Multi-level controllers for MVC architecture

查看:200
本文介绍了用于MVC架构的多级控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚遇到了我目前用于我的应用程序的MVC架构的限制之一。目前,我的网址如下所示:

  www.example.com/controller/action 

每个请求到达前端控制器,从URL加载请求的控制器类,并执行它的操作(方法)。



例子:
有一个'users'控制器,它保存的方法有createUser(),editUser ),deleteUser()等。所有完全可能的当前URL结构...但是如果我们还需要管理用户类型呢?我们需要一个单独的控制器'usertypes',它还包含createUserType(),editUserType()等方法。但是,由于用户类型是用户的一部分,所以'usertypes'控制器应该嵌套在用户控制器中, :

  www.example.com/users/usertypes/addusertype 

对于当前的URL结构,这是不可能的...如何使用嵌套(或多层次如果你会)控制器?



更新:这是我正在处理的应用程序的基本表示:它是管理部门的一个基本业务应用程序,他们可以从3个类别添加,查看,编辑和删除数据(促销,邮寄和持卡人)。每个类别表示数据库中的一个表,并有自己的不同字段。帐户需要由管理员创建,用户不能自己创建帐户,他们不能查看他们的用户个人资料。



对于每个类别,我已经控制器,它包含add(),edit(),getAll(),getSingle(),delete()等操作。每个操作从模型调用相应的方法并渲染相应的View。 p>

这是所有可能的当前URL结构,其URL类似:

  example.com/promotions/add 
example.com/promotions/getsingle?id=123

最近,他们要求我也可以管理类型的促销,邮寄和持卡人。现在他们已经有一个学校折扣,20%的折扣等...但他们想添加更多,因为他们的愿望。



这意味着我需要一个PromotionTypes控制器,它还包含add(),getAll(),getSingle(),delete如果PromotionTypes控制器可以嵌套在原始促销控制器中,则启用网址如下:

  example.com/promotions/promotiontypes / add 

而不是

  example.com/promotiontypes/add 

是不可能的,因为URL的第一部分自动被视为控制器,而第二部分作为从其执行的动作。

解决方案

似乎您已将控制器绑定到视图,而是绑定到每个域对象



此外,奇怪的路由是什么?为什么不:

  POSTwww.example.com/profile/42/type

因为您正在向特定用户的配置文件添加类型。这转换为在配置文件控制器上执行 postType()



如果您正在构建自己的路由机制,也许此答案可能会有所帮助。



底线是:你不需要这样奇怪的控制器的控制器。你需要的是开始查看你有什么样的视图,然后为每个视图创建控制器,而不是通过查看模型层开始。


I just encountered one of the limitations of the MVC architecture I'm currently using for my applications. Currently, my URLs look like this:

www.example.com/controller/action

Each request arrives at the front controller, which loads the requested controller class from the URL, and executes the action (method) of it. This works fine, until you need to start using nested controllers.

Example: There's a 'users' controller which holds methods like createUser(), editUser(), deleteUser() etc. All perfectly possible with the current URL structure... But what if we also need to manage user types? We would need a separate controller 'usertypes' which also holds methods like createUserType(), editUserType()... However, since user types are a part of users, the 'usertypes' controller should be nested inside the users controller, as this:

www.example.com/users/usertypes/addusertype

With the current URL structure however, this is not possible... How can I make use of nested (or multi-level if you will) controllers?

UPDATE: here's a basic representation of the application I'm working on: It's a basic business application for the administration department where they can add, view, edit and delete data from 3 categories (Promotions, Mailings and Cardholders). Each category represents a table in the database and has its own distinct fields. Accounts need to be created by the admin, users cannot create an account themselves and they can't consult their user profile.

For each of those categories I've made a controller, which holds actions like add(), edit(), getAll(), getSingle(), delete()... Each of those actions calls the appropriate methods from the Model and renders the corresponding View(s).

This was all possible with the current URL structure, which had URL's like:

example.com/promotions/add
example.com/promotions/getsingle?id=123

Recently they asked me to make it possible to also manage types of promotions, mailings and cardholders. Right now they already have a School Discount, a 20% Discount etc... But they want to add more as they wish.

This means i need a PromotionTypes controller, which also holds actions like add(), getAll(), getSingle(), delete()... It would be nice if the PromotionTypes controller could be nested in the original promotions controller, which enables URL's as so:

example.com/promotions/promotiontypes/add

instead of

example.com/promotiontypes/add

With my current front loader, this is not possible, since the first part of the URL automatically gets treated as the controller, and the second part as the action to execute from it.

解决方案

It seems like you have tied the controllers not to the view but to each domain object.

Also, what's with the strange routing? Why not :

POST "www.example.com/profile/42/type"

Because you are adding a types to the profile of specific user. This translates to execution of method postType() on the Profile controller.

If you are building your own routing mechanism, maybe this answer could be helpful.

The bottom line is this: you do not need such strange controller-of-controllers. What you need is to start looking at what sort of views you have , and then creating controller for each of them, instead of starting by looking at model layer.

这篇关于用于MVC架构的多级控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆