when should i use multiple controllers in mvc?

2019-07-07 19:22发布

are there times you might want to use multiple controllers in mvc?

eg.

/controllers/foo.php

/controllers/bar.php

or

/controllers/foo/baz1.php

/controllers/foo/baz2.php

/controllers/bar/baz1.php

/controllers/bar/baz2.php

could someone give some examples WHEN i might want to do that and some example controller names.

one occasion i thought about might be when you got a main site (for users) and a admin site (for customers).

all feedbacks and suggestions are appreciated

4条回答
何必那么认真
2楼-- · 2019-07-07 19:30

In your application if you are working with User, Employee, Department then you'll define them in 3 classes, right? now make controller for each those classes : UserController, EmployeeController, DepartmentController etc.

example:

User/Add/
User/Edit/1/
User/Delete/1/

Employee/Add/
Employee/Edit/1/
Employee/Delete/1/

Department/Add/
Department/Edit/1/
Department/Delete/1/

查看更多
Lonely孤独者°
3楼-- · 2019-07-07 19:31

pretty much whenever you have a different task to complete. If you have something that involves handling users, name your controller users.whatever, then name the actions appropriately (create, edit, update, delete, search, etc.). If you have something that solely does searching, name it search.whatever. Etc. An easy way to remember this sort of thing is from the RESTful RFC (sorry, no clue what the actual RFC number is for that one), something along the lines of something.com/noun/verb where noun == the all-encompassing "thing" this controller handles and verb == the action being performed (see above). This is one method at least.

查看更多
在下西门庆
4楼-- · 2019-07-07 19:32

Usually controllers deal with models, which represent corresponding database tables. So if you have tables users and posts, your app will have models User and Post, and therefore controllers Users and Posts. That is typical RoR way, which used in many PHP MVC frameworks. URLs in such application looks as follow:

/controller/action/parameter1/parameter2/...
i.e.
/users/edit/1/
or
/posts/new/

And actions corresponds to controller class methods. Actually I think it became de-facto standart in MVC architecture, becuase it looks natural and logical.

查看更多
太酷不给撩
5楼-- · 2019-07-07 19:43

Have a look at the source for Nerd Dinner, http://nerddinner.codeplex.com/, has multiple controllers.

查看更多
登录 后发表回答