开发者

How to decide on controllers for web application?

开发者 https://www.devze.com 2023-01-15 14:52 出处:网络
I use php and usually structure my application into model-view-controller so its always accessed via index.php with class and method attributes. Class attribute passed as part of URL specifies control

I use php and usually structure my application into model-view-controller so its always accessed via index.php with class and method attributes. Class attribute passed as part of URL specifies controller class and method simply method to be called. 开发者_如何学GoThis seems to be pretty common, but then I'm always having trouble in figuring out what controllers shall I create. What is the best, easiest and most applicable way to decide on what controllers should be created? I understand it depends on web application itself but must be some general way of thinking to get this process started.


I've found that building controllers based on your application's objects works well, and can take care of most actions you'll want for your app.

Take a look at SO -- there's URLs starting with /questions, /tags, /users, etc. I'd suggest a design which starts by creating a different controller for each object. /questions (or /questions/list) returns a list of all the questions. /questions/[0-9]+ returns the details of a particular question with that id number. /questions/ask returns the Ask Question interface.

As you continue building your app, you might find that the controller-based-on-objects method doesn't meet all your needs. For example, on my site (http://www.wysiap.com), I eventually made a /list controller to simplify my Grails URL mapping. But in most cases I did use this method and it's easy to figure out which controller should be doing different actions.


I recommend to think about the pages you'll need in your applications to accomplish all the requested tasks. You'll group similar tasks on the same page and create as many pages as you need. A page can be sliced in different views for specific actions.

With this in mind you could have one controller per page. Each view of the page can have its own method (action) in the controller. And inside the method of each view you can have a switch() that will enable you to have several tasks for the view. Example:

  • index.php (Dashboard controller)
  • /question-list (QuestionList controller, action index, display the whole page)
  • /question-list/add (QuestionList controller, action add, manage the "add" view with tasks like show-form, validate-form, insert-question)
  • /profile (Profile controller, action index)
  • /profile/edit (Profile controller, action edit, manage all the tasks requested for your profile)
  • ...

I design most of my web applications this way and using Zend-Framework

0

精彩评论

暂无评论...
验证码 换一张
取 消