You are on page 1of 10

What is MVC?

A Primer in 10 Slides

By Dominique Gerald M Cimafranca

dominique.cimafranca@gmail.com
http://villageidiotsavant.com

Model-View-Controller

an architectural

pattern in software engineering

a way of designing and building applications

object-oriented software development

separates application logic from presentation

loose coupling between components

Without MVC
delete.php

All in one program!

update.php
read.php

presentation
database

database

create.php
application

Client

http://myapp/create.php
http://myapp/read.php
http://myapp/update.php
http://myapp/delete.php

db=connect(database);
var1=$_POST[data1];
var2=$_POST[data2];
db.query(insert into db values var1,var2);
print <HTML><H1>Success</H1></HTML>;

What happens when you...

change the database settings?

change the database design?

change the database?

process more variables?

change the response?

change the look and feel?

How does MVC work?


customer (controller)
function create() {
:
:
}
function read() {
:
:
}
function update() {
:
:
}

Client

function delete() {
:
:
}

http://myapp/customer/create
http://myapp/customer/read
http://myapp/customer/update
http://myapp/customer/delete

function read($id) {
Customer->get($id);
$data=Customer->read();
:
:
render($data,template.tpl);
}

Controller handles application logic

A single controller may have multiple methods

A controller collects all the actions that takes place

Several controllers may comprise an application

How does MVC work?


Customer
customer (controller)
function create() {
:
:
}
function read() {
:
:
}
function update() {
:
:
}

Client

function delete() {
:
:
}

http://myapp/customer/create
http://myapp/customer/read
http://myapp/customer/update
http://myapp/customer/delete

name
category
quantity
:
read()
save()
:

function read($id) {
Customer->get($id);
$data=Customer->read();
:
:
render($data,template.tpl);
}

Controller invokes models

Controller manipulates model attributes

Controller uses model methods

Models can invoke other models

How does MVC work?


Customer

Charlie Brown
Lucy Van Pelt
Linus Van Pelt

customer (controller)
function create() {
:
:
}
function read() {
:
:
}
function update() {
:
:
}

Client

name
category
quantity
:
read()
save()
:

function read($id) {
Customer->get($id);
$data=Customer->read();
:
:
render($data,template.tpl);
}

function delete() {
:
:
}

http://myapp/customer/create
http://myapp/customer/read
http://myapp/customer/update
http://myapp/customer/delete

Controller combines data with


template
Template may perform
additional processing
Return combination as HTML

$header
$sidebar

::::

Our Customers

for($item in $data) {
echo $item;
}

Or we could do it with XML

JavaScript
C / C++
Java
Whatever...

Customer

customer (controller)
function create() {
:
:
}

XML request

XML response
Client

function read() {
:
:
}
function update() {
:
:
}

name
category
quantity
:
read()
save()
:

function read($id) {
Customer->get($id);
$data=Customer->read();
:
:
render($data,template.tpl);
}

function delete() {
:
:
}

Client does not need to be a web browser

Can be any client that speaks XML

Serialization: conversion of data into bits

With this, we can talk about web services

$xml->serialize($data)

XML, JSON, YAML, ...

Some MVC web frameworks

Ruby on Rails (www.rubyonrails.org)

CakePHP (www.cakephp.org)

CodeIgniter (www.codeigniter.org)

Yii (www.yiiframework.com)

Django (www.djangoproject.org)

CherryPy (www.cherrypy.org)

Spring MVC (www.springsource.org)

Catalyst (www.catalyst.org)

Mojolicious (www.mojolicio.us)

Frameworks
make things
easier
BUT
you don't need
to use them to
practice MVC

License

You are free:

to Share: to copy, distribute and transmit the work

to Remix: to adapt the work

Under the following conditions:

Waiver: Any of the above conditions can be waived if you get permission from the copyright holder.
Public Domain: Where the work or any of its elements is in the public domain under applicable law, that
status is in no way affected by the license.

Other Rights: In no way are any of the following rights affected by the license:

Your fair dealing or fair use rights, or other applicable copyright exceptions and limitations;

The author's moral rights;

Share Alike: If you alter, transform, or build upon this work, you may distribute the resulting work only under
the same or similar license to this one.

With the understanding that:

Attribution: You must attribute the work in the manner specified by the author or licensor (but not in any way
that suggests that they endorse you or your use of the work).

Rights other persons may have either in the work itself or in how the work is used, such as publicity or
privacy rights.

Notice: For any reuse or distribution, you must make clear to others the license terms of this work.
The best way to do this is with a link to this web page.

You might also like