What is MVC?
A Primer in 10 Slides
Dominique Gerald M Cimafranca [email protected]
http://villageidiotsavant.com
By
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
Client
delete.php
update.php
read.php
create.php
database
db=connect(database);
var1=$_POST[“data1”];
var2=$_POST[“data2”];
db.query(“insert into db values var1,var2”);
print “<HTML><H1>Success</H1></HTML>”;
http://myapp/create.php
http://myapp/read.php
http://myapp/update.php
http://myapp/delete.php
database
application
presentation
All in one program!
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?
Client
http://myapp/customer/create
http://myapp/customer/read
http://myapp/customer/update
http://myapp/customer/delete
customer (controller)
function create() {
:
:
}
function read() {
:
:
}
function update() {
:
:
}
function 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?
Client
customer (controller)
function create() {
:
:
}
function read() {
:
:
}
function update() {
:
:
}
function delete() {
:
:
}
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
name
category
quantity
:
read()
save()
:
Customer
http://myapp/customer/create
http://myapp/customer/read
http://myapp/customer/update
http://myapp/customer/delete
How does MVC work?
Client
customer (controller)
function create() {
:
:
}
function read() {
:
:
}
function update() {
:
:
}
function delete() {
:
:
}
function read($id) {
Customer->get($id);
$data=Customer->read();
:
:
render($data,”template.tpl”);
}
name
category
quantity
:
read()
save()
:
Customer
http://myapp/customer/create
http://myapp/customer/read
http://myapp/customer/update
http://myapp/customer/delete
$header
$sidebar
for($item in $data) {
echo $item;
}
Our Customers
: : : :
Charlie Brown
Lucy Van Pelt
Linus Van Pelt
Controller combines data with
template
Template may perform
additional processing
Return combination as HTML
Or we could do it with XML
Client
customer (controller)
function create() {
:
:
}
function read() {
:
:
}
function update() {
:
:
}
function delete() {
:
:
}
function read($id) {
Customer->get($id);
$data=Customer->read();
:
:
render($data,”template.tpl”);
}
name
category
quantity
:
read()
save()
:
Customer
XML request
XML response
$xml->serialize($data)
Whatever...
Java
C / C++
JavaScript
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, 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)
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:
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).
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.