1

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.

4

2 回答 2

2

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

另外,奇怪的路由是怎么回事?为什么不 :

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

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

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

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

于 2012-08-15T00:26:17.477 回答
1

您没有提及您是否使用框架,但通常的方法是让“路由器”对异常应用特殊处理,例如Zend Framework 路由器

于 2012-08-14T23:52:40.703 回答