开发者

MVC organization for a blog

开发者 https://www.devze.com 2023-01-10 00:21 出处:网络
I\'m still just getting into MVC, and for my first real project I plan on creating a blog. This will be extremely basic (at least at first). Everything I need is going to be on the same page. Here are

I'm still just getting into MVC, and for my first real project I plan on creating a blog. This will be extremely basic (at least at first). Everything I need is going to be on the same page. Here are the initial features I am shooting for:

  • User should be able to log in, but not register (I will be the only one able to post, and I added myself directly to the DB.
  • Blog posts should be listed in descending order with a Title, a post date, and the body. No comments required for now.
  • The bottom of the page will always have an area to make a new post, assuming you're logged in.

Since I'm still new to the MVC structure, I'd like some advice on how it should be organized.

For my models, I figured I should have a post repository and a BlogPost class for the post data which can be used for both t开发者_如何学Gohe posting and retrieving. I would also need a class for the user.

When it comes to controllers I'm a bit less confident. Should I have a different controller for every type of action? For example, posting should have a controller, retrieving should have a controller, logging in should have a controller, etc?

As for the views, since I really only need a single page, should I only have a single view and have that view output the appropriate content from my controllers?

Just let me know if I'm on the right track, I suppose. If my thought process is way off, please tell me. I've just started working my way through Steven Sanderson's MVC 2 book, but I feel like I need to go out on my own and play between my reading sessions.

Thanks.


Controllers should be grouped by functionality. You could also have a controller per resource (REST). You could have an AuthenticationController which handles authentication and PostsController which will handle the posts retrieval and adding a new post. As far as the views are concerned assuming you will have a single page that will list posts and add new posts you could have a single view but maybe with multiple partial views/editor/display templates.

0

精彩评论

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

关注公众号