tags:

views:

102

answers:

4

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

+1  A: 

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.

dhoss
+4  A: 

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.

Sergei
but what if you want to do a thing that will not access only 1 table, but multiple. eg. if i create a thread that i have tagged, then it spans over 3 tables (threads, tags and thread_tag_map). a typical many-to-many relationship. what controller would be used, and what model/s? could you please describe the flow in this case?
never_had_a_name
Models are related to each other. Imagine if you showing post with /posts/view/1/. It means that controller is used is Posts, and model is Post, WHICH accesses related models Tag and Comment. Related models can access each other's methods for retrieving the data.
Sergei
+1  A: 

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

Jonathan
nothing beats a real life example!
never_had_a_name
+1  A: 

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/

Muktadiur Rahman
but if i got method for everything, cant a user type this into the browser and it will delete things?
never_had_a_name
you have to check that the request is authorized or not. if unauthorized then redirect to login page. you have to write a method for that.In ASP.NET MVC if u use [Authorize] attribute with your methods(Actions) then that will prevent unauthorized access.
Muktadiur Rahman