I'm currently creating a simple application quite similar to the timetracker demo from Prado. As this is my first "real" Yii project i came across a simple question: How many controllers should i split my application into?
I’ll try to give an example. For simple tables like Projects i’ve used the CRUD command and get a controller for each table. That’s fine and is a good starting point.
Now i have another table Times that will be used for two different things:
a) Storing the times, each staff member has worked on a project.
Creating statistics for the management
Both a) and should be feature rich UIs that make it as easy as possible to track project times. So both might each need a number of different views and both might get enhanced with some “convenience” features (e.g. start/stop timer, “copy from yesterday”, etc.).
I wonder, if i should create two separate Controllers or put everything into one huge TimeController.
The above is only an example. It would be interesting if there's a more general rule of thumb on how to model your controllers.
You may treat a controller as a module. The real code actually resides in individual actions. If you put action code in external action classes, your controller class will remain slim, and you also have the possibility of reusing action classes.
Having said that, there is not strict rule on how to divide controllers. For your situation, I would put everything into a single controller.
I usually split my controllers into sections if i have a section. so i will create a folder 'members' and under that folder will add as many controllers as i like that deals with members stuff. Like Members Management, Searching Members, Groups Management, Permissions, etc… if you ask me i would create a folder 'times' inside the controllers folder and have 2 controllers in it one for each section. I also like to use other components that i include in the components directory that i could access them within each controller.
I agree to split things up but use CActions not many CControllers. By using CActions you will get more millage from your code. And if you decide that you wanted to make a "portlet" most of your work will already be done.
I seem to think of controllers as master objects. A SiteController would control the entry page and related triggers to other pages. If I have a Search page that returned a listing, filtering of the the listing, etc, I would have a ListController. A member area would probably have another Controller. It seems to make logical sense to me that major functionality should be chunked into controllers. This is how I see it, but am definitely looking for other opinions since I've struggled with the concept.
Ok, thanks for all your quick suggestions. I think, this is a quite interesting discussion, as different approaches can be examined.
I think i'll start with a single TimeController first. Reorganizing code later shouldn't be hard if required.
That’s what i really like about Yii (and also Prado): You can write very well structured and maintainable code. Once again a big THANK YOU (mostly to Qiang i guess…) for sharing those great frameworks with us. They help so much and from both i was able to really learn a lot about organizing your code.
In most cases, a controller is about a subject and thus its name is a noun (such as post, time). Controller consists of a collection of actions that are operations can be applied by end-users to the subject. I think that may be considered as a general guideline on how to divide controllers.
In cases when a subject appears in different domains (e.g. backend, frontend), we may use different controllers for it, but this is not a strict requirement.