webdevRefinery Forum: An introduction to MVC architecture - webdevRefinery Forum

Jump to content

Think a topic deserves its own subforum?

Any topic that gets popular here will have a subforum made for it, as long as there are folks around who can answer questions! So if you think wdR is missing something, just talk about it here :)
  • 3 Pages +
  • 1
  • 2
  • 3
  • You cannot start a new topic
  • You cannot reply to this topic

Rate Topic: -----

User is online Kyek 

  • Founder of wdR
  • Group: Administrators
  • Posts: 4834
  • Joined: 20-February 10
  • LocationPhiladelphia, PA, USA
  • Expertise:HTML,CSS,PHP,Java,Javascript,Node.js,SQL

Posted 10 March 2010 - 09:13 PM (#1)

An introduction to MVC architecture


The cleanest way to write all webapps
An Introduction to MVC Architecture
A Guide by Kyek
Written March 10, 2010



Most... Valuable... Cake?
MVC stands for Model, View, Controller. For years, it's been the standard for how to split the code of a web application, and for good reason -- once you try it, you'll see that there's just no better way to organize the pieces of your project. It makes your work faster, more maintainable, and much easier to reuse in other places.


Understanding the pieces
The easiest way to understand MVC is to imagine someone typing your website's URL into their browser, and that page request hitting your web application. From there, here's how the pieces of the MVC puzzle come together:

Controller: The "Controller" takes the request and reads it, figuring out what the user wants to see. Looking at the URL and the data that was passed along, it appears that the user is trying to look at page two of your blog. Great, the controller's main job is now done! It's identified what needs to be delivered. Now it has two other tasks to do: It has to create the Model, and pass it to the correct View. What are they, you ask? Read on!

Model: The Model is a fancy name for the object containing all the necessary data for the page. In our example, the user has requested page two of your blog. So the controller tells the model "Load Blog page 2 from the database!". The Model's job is to do ANY data-related related task, from database communication to file access. Then it stores that data inside of itself -- all the data needed to construct the page.

View: Now that the controller has identified what's being asked for AND it has the model ready to go, it selects a View. The 'views' are the different display templates for your site. You could have a view for your site's landing page, a view for a blog post's comment section, and a view for pages of blog posts. The view has, at minimum, all the HTML that's needed to display a page, and just enough code inside of it to use the data that's being provided in the model. So in our example, the Controller will send the Model that it has ready to the Pages_Of_Blog_Posts view. The view looks like a normal HTML page, but there's a little bit of code to dig into the model and show that it's page "2" we're looking at. There's a little bit of code to loop through each blog post that's been returned, dumping in the name and a snippet of its text. There's a little bit of code to see that we're on page 2 and that there's a total of 5 pages, so it knows to put in both the "Previous Page" and "Next Page" links.


Wow Kyek, that almost seems too simple!
Well, it's a little more complex than that. Let's take the controller, for example. Modern web applications have a set of controllers -- one to handle each kind of request. Requests for blog pages get sent to the Blog Page controller. Requests for the Blog Comments get sent to the Blog Comments controller. Requests to search the blog goes to the Blog Search controller, and so on. So how do the requests GET to each of those controllers? Well, you need some sort of dispatcher that reads the request and knows which controller it should go to. That, itself, is also part of the "Controller" side of your web application.

The Model is a BIG one that has a few different interpretations. Originally, the word "model" referred to only the data that gets passed to the View, and in some contexts, that's still what people mean when they say "model". But for modern web applications, the Model part of the MVC structure isn't just the data, it's also allll the logic involved in retreiving, saving, writing, or manipulating that data. "Fat Model, Skinny Controller" is a common phrase used to describe where the heavy lifting should happen in your webapp. Controllers should only figure out what type of model needs to be called on -- the model should do every last bit of the data work. So if you're using a database wrapper to support more than one type of database, for example, that's still considered part of your Model.

The View seems like the simplest part, right? Just HTML with some code thrown in to dump out the model's data. But it's often anything but simple. Think about a full, polished website. It probably has a header that appears on every page, right? And maybe a sidebar that's on more than one page, and other parts that are shared between different pages. The first rule of programming is to never have duplicate code, so for the view, you generally want things like your header and your sidebar and your footer all in their own separate files, which can be dynamically included into your main content pages. So the view for our example (loading page two of your blog) may look something like this (just an example here, not a real language):
INCLUDE PAGE_HEADER
INCLUDE LEFT_SIDEBAR
<< Code to display blog posts here >>
INCLUDE GOOGLE_ANALYTICS
INCLUDE FOOTER


And sometimes the people making these views aren't coders, so instead of letting them work with very sensitive PHP or Java or Python, you'll want a mini scripting language for them to use that you'll parse with the controller so that you can be sure these people can't break your site.


Sound like it's too much? It's not, if you start easy!
If you've never built an MVC-structured webapp before, don't be scared off by the complications! You can build very simple and tiny apps with MVC architecture without getting into all that complicated-ness yet. Make a simple controller that can tell what's being requested, make a simple model that only supports one way of storing data, and make views that are simple HTML files that might reuse code here and there. The beauty of starting with that MVC structure in mind is that, at any time, you can make any of those parts more complex and more featureful without ever needing to touch any of the other parts!

So after your app is built, then spend the time to make your Model faster at getting and storing data, and your controllers and views won't care. Then maybe you'll decide to support multiple themes in your view, and your models and controllers won't care about that. You can do upgrades however, wherever, and whenever you like without risking breaking anything. So start easy, then work on making things more advanced!


When should I try this?
If you have a basic understanding of any server-side web language, there's no reason to wait! With how very simply you can do this, it's a great way to start learning. So from here on out, don't build any more webapps without thinking about how to split it up into those three pieces :)


Go forth and code better!
7


User is offline AjBlue 

  • Group: Members
  • Posts: 172
  • Joined: 08-March 10

Posted 10 March 2010 - 09:22 PM (#2)

I beg to differ kyek, when i think of model i think of this
Posted Image
Posted Image
4


User is online Kyek 

  • Founder of wdR
  • Group: Administrators
  • Posts: 4834
  • Joined: 20-February 10
  • LocationPhiladelphia, PA, USA
  • Expertise:HTML,CSS,PHP,Java,Javascript,Node.js,SQL

Posted 10 March 2010 - 09:26 PM (#3)

Your architecture is bad. The model is supposed to be fat ;-)
5


User is offline Rob 

  • Group: Members
  • Posts: 203
  • Joined: 08-March 10
  • Expertise:HTML,CSS,PHP,Javascript,SQL,Graphics

Posted 10 March 2010 - 10:04 PM (#4)

Very nice introduction Kyek!

As I see it, there are only a couple of critical parts that tend to complicate things. What they do is not that complicated, but it is critical that they be well designed and tested.

  • Request Router - Typically used by the front end controller to route the request to the correct request controller.
  • URI Helper - Typically used to dynamically build the correct links for the UI and assist the front end controller by parsing the URI into it's component parts.
  • Error Handler - Typically used to handle any error that occurs, decide what to do in the case of an error, which could include one of more of: Displaying an error to the user, logging the error or notifying an administrator of the problem.

The error handler, is typically a specialized controller designed to produce a useful (friendly) error message for the end user, while preventing information leakage, which could compromise the security of your site. It will usually log at least some of information about the error, which can be used forensically, to determine the cause of the error or provide the information necessary to recreate the error.


0


User is offline heLLo 

  • Group: Members
  • Posts: 70
  • Joined: 10-March 10

Posted 13 March 2010 - 12:56 AM (#5)

What do your recommend knowing before you start this? I mean, I barely understood this and i'm ok in HTML and CSS.

heLLo
Posted Image
0


User is offline XingChow 

  • Group: Members
  • Posts: 189
  • Joined: 09-March 10
  • Expertise:HTML,CSS

Posted 13 March 2010 - 01:39 AM (#6)

heLLo: If you only know HTML then this is nothing for you. It's for PHPers and other SSSripters.

Kyek: Beautiful guide. Once I master my PHP I will defiantly do it like this.
Posted Image
0


User is offline AwesomezGuy 

  • 兄ジャック
  • Group: Members
  • Posts: 1121
  • Joined: 08-March 10
  • LocationIreland
  • Expertise:HTML,CSS,PHP,Javascript,Python,SQL

Posted 13 March 2010 - 05:13 AM (#7)

View PostKyek, on 10 March 2010 - 09:26 PM, said:

Your architecture is bad. The model is supposed to be fat ;-)

That made me lol so hard.
0


User is online Kyek 

  • Founder of wdR
  • Group: Administrators
  • Posts: 4834
  • Joined: 20-February 10
  • LocationPhiladelphia, PA, USA
  • Expertise:HTML,CSS,PHP,Java,Javascript,Node.js,SQL

Posted 13 March 2010 - 07:03 AM (#8)

View PostheLLo, on 13 March 2010 - 12:56 AM, said:

What do your recommend knowing before you start this? I mean, I barely understood this and i'm ok in HTML and CSS.

heLLo

Xing hit the nail on the head -- you can use this structure in any server-side language. So once you start learning PHP or Java or even Python or Ruby, you can make sure your database stuff stays separate from your HTML stuff and make your life way easier in the future :)


View PostXingChow, on 13 March 2010 - 01:39 AM, said:

Kyek: Beautiful guide. Once I master my PHP I will defiantly do it like this.

Awesome :). This is great even while you're still learning, too. When you're writing up your little pages to get your feet wet with databases and all that sort of thing, practice doing it in a separate file and go from there :). It seems ridiculous to worry about that while you're learning, but getting the process in your head that early means you'll start thinking that way later. And that will make you a badass coder :)
0


User is offline XingChow 

  • Group: Members
  • Posts: 189
  • Joined: 09-March 10
  • Expertise:HTML,CSS

Posted 13 March 2010 - 08:10 AM (#9)

Thanks for correcting me xD

Yay I hit a nail. This is also the first post where Kyek only talks about ME :D .
Posted Image
0


User is offline Rob 

  • Group: Members
  • Posts: 203
  • Joined: 08-March 10
  • Expertise:HTML,CSS,PHP,Javascript,SQL,Graphics

Posted 13 March 2010 - 06:10 PM (#10)

View PostKyek, on 13 March 2010 - 07:03 AM, said:

Xing hit the nail on the head -- you can use this structure in any server-side language. So once you start learning PHP or Java or even Python or Ruby, you can make sure your database stuff stays separate from your HTML stuff and make your life way easier in the future :)



Awesome :). This is great even while you're still learning, too. When you're writing up your little pages to get your feet wet with databases and all that sort of thing, practice doing it in a separate file and go from there :). It seems ridiculous to worry about that while you're learning, but getting the process in your head that early means you'll start thinking that way later. And that will make you a badass coder :)




Kyek is absolutely correct! If you learn to develop using the MVC design pattern, there is no "unlearning" that ever need take place. Design patterns save tons of work in the long run, making you more productive. Productivity is key to optimizing your time and efforts in profitable ways, not necessarily monetarily. Profitability can be measured in other ways, such as being able to spend more time with friends and family instead.

0


User is offline XingChow 

  • Group: Members
  • Posts: 189
  • Joined: 09-March 10
  • Expertise:HTML,CSS

Posted 13 March 2010 - 06:15 PM (#11)

Wait. Did you just say Kyek can be wrong? He can't, Kyek is ALWAYS right.
Posted Image
0


User is offline Rob 

  • Group: Members
  • Posts: 203
  • Joined: 08-March 10
  • Expertise:HTML,CSS,PHP,Javascript,SQL,Graphics

Posted 13 March 2010 - 06:19 PM (#12)

View PostXingChow, on 13 March 2010 - 06:15 PM, said:

View PostRob, on 13 March 2010 - 06:10 PM, said:

Kyek is absolutely correct!

Wait. Did you just say Kyek can be wrong? He can't, Kyek is ALWAYS right.





If you were actually asking me, I think the above answers your question, no?


0


User is offline hydralisk 

  • Group: Administrators
  • Posts: 495
  • Joined: 02-March 10
  • Expertise:HTML,CSS,Javascript,Flash

Posted 17 March 2010 - 07:07 AM (#13)

Technically, by saying Kyek is right, you're implying that he COULD have been wrong. Rather than saying Kyek is right, just say "Kyek is Kyek."
0


User is offline MrSaints 

  • Group: Members
  • Posts: 7
  • Joined: 28-March 10
  • Expertise:HTML,CSS,PHP,Java,Javascript,Ruby on Rails,SQL,Graphics

Posted 28 March 2010 - 02:44 AM (#14)

And now you should introduce the one and only, 'Hierarchical-Model-View-Controller (HMVC)' :):)
0


User is online Kyek 

  • Founder of wdR
  • Group: Administrators
  • Posts: 4834
  • Joined: 20-February 10
  • LocationPhiladelphia, PA, USA
  • Expertise:HTML,CSS,PHP,Java,Javascript,Node.js,SQL

Posted 28 March 2010 - 06:55 AM (#15)

rofl-- I'll go there when someone is ready for it xD
0


User is offline Celyst 

  • Group: Members
  • Posts: 78
  • Joined: 08-March 10
  • LocationSingapore
  • Expertise:Java,SQL

Posted 02 April 2010 - 03:01 AM (#16)

Crap, I wished I had read this post a lot earlier. My project is already 80% complete and I've been going all along with approximately a 70% understanding of how the MVC architecture works. Thankfully it all worked out in the end and I have something that's more or less like what you've described.
0


User is online Kyek 

  • Founder of wdR
  • Group: Administrators
  • Posts: 4834
  • Joined: 20-February 10
  • LocationPhiladelphia, PA, USA
  • Expertise:HTML,CSS,PHP,Java,Javascript,Node.js,SQL

Posted 04 April 2010 - 01:33 PM (#17)

Really, as long as the Model is separated from the other two, that's the most important part :). You can change everything else to your heart's content, but as long as your model stays stable, you're in the clear. Getting the view and controller separate just makes things easier to work with later down the line :)
0


User is online Mack 

  • http://mackgoodstein.com/
  • Group: Members
  • Posts: 1961
  • Joined: 08-March 10
  • Expertise:HTML,CSS,PHP,Javascript

Posted 04 April 2010 - 01:35 PM (#18)

View Postdoody, on 02 April 2010 - 03:01 AM, said:

Crap, I wished I had read this post a lot earlier. My project is already 80% complete and I've been going all along with approximately a 70% understanding of how the MVC architecture works. Thankfully it all worked out in the end and I have something that's more or less like what you've described.


Well, according to "them" that means you still have 80% left, so you have plenty of time to improve it
0


User is offline Celyst 

  • Group: Members
  • Posts: 78
  • Joined: 08-March 10
  • LocationSingapore
  • Expertise:Java,SQL

Posted 05 April 2010 - 12:02 AM (#19)

I'm doing JSP with servlets, so I've more or less figured out that the JSP page is the view. It's just the model and controller part that's a bit fuzzy. Right now I have a servlet (controller) that takes the input from the page and passes it to the data manager (model), which queries the DB and loads the information into a Java bean, before passing that back to the controller, which passes it on to the JSP page.

I was just worried that the model part was getting very huge, but I guess everything should be ok now!
0


User is online Kyek 

  • Founder of wdR
  • Group: Administrators
  • Posts: 4834
  • Joined: 20-February 10
  • LocationPhiladelphia, PA, USA
  • Expertise:HTML,CSS,PHP,Java,Javascript,Node.js,SQL

Posted 05 April 2010 - 06:26 AM (#20)

That sounds perfect :) The model part SHOULD be very huge, so you're set! The more you're capable of doing inside of the model (without leaving the scope of what the model should be), is the more code you don't have to deal with in the controller and view. That makes maintaining and reworking your site so very very much easier later down the line.
0


Share this topic:


  • 3 Pages +
  • 1
  • 2
  • 3
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Enter your sign in name and password


Sign in options
  Or sign in with these services