3.
Model-View-ControllerModel-View-Controller
Most used architectural pattern for today's webMost used architectural pattern for today's web
applicationsapplications
Originally described in terms of a design patternOriginally described in terms of a design pattern
for use with Smalltalk by Trygyve Reenskaug infor use with Smalltalk by Trygyve Reenskaug in
19791979
The paper was published under the titleThe paper was published under the title
”Applications Programming in Smalltalk-80:”Applications Programming in Smalltalk-80:
How to use Model-View-Controller”How to use Model-View-Controller”
MVCMVC
5.
Responsible for managing the dataResponsible for managing the data
Stores and retrieves entities used by anStores and retrieves entities used by an
application, usually from a database but can alsoapplication, usually from a database but can also
include invocations of external web services orinclude invocations of external web services or
APIsAPIs
Contains the logic implemented by theContains the logic implemented by the
application (business logic)application (business logic)
ModelModel
6.
Responsible to display the data provided by theResponsible to display the data provided by the
model in a specific formatmodel in a specific format
View (Presentation)View (Presentation)
7.
Handles the model and view layers to workHandles the model and view layers to work
togethertogether
Receives a request from the client, invokes theReceives a request from the client, invokes the
model to perform the requested operations andmodel to perform the requested operations and
sends the data to the viewsends the data to the view
ControllerController
12.
The application entry point will be index.phpThe application entry point will be index.php
The index.php file will delegate all the requestsThe index.php file will delegate all the requests
to the controllerto the controller
Entry point - index.phpEntry point - index.php
13. require_once("model/Model.php");require_once("model/Model.php");
cclass Controller {lass Controller {
public $model;public $model;
public function __construct()public function __construct()
{{
$this->model = new Model();$this->model = new Model();
}}
// continued...// continued...
Controller – controller/Controller.phpController – controller/Controller.php
14. require_once("model/Model.php");require_once("model/Model.php");
cclass Controller {lass Controller {
public $model;public $model;
public function __construct()public function __construct()
{{
$this->model = new Model();$this->model = new Model();
}}
// continued...// continued...
Controller – controller/Controller.phpController – controller/Controller.php
15. pupublic function invoke()blic function invoke()
{{
if (!isset($_GET['book']))if (!isset($_GET['book']))
{{
// no special book is requested, we'll// no special book is requested, we'll
show a list of all available booksshow a list of all available books
$books = $this->model->getBookList();$books = $this->model->getBookList();
include 'view/booklist.php';include 'view/booklist.php';
}}
elseelse
{{
// show the requested book// show the requested book
$book = $this->model->getBook$book = $this->model->getBook
($_GET['book']);($_GET['book']);
include 'view/viewbook.php';include 'view/viewbook.php';
}}
}}
}}
Controller – controller/Controller.phpController – controller/Controller.php
16.
The controller is the first layer which takes aThe controller is the first layer which takes a
request, parses it, initializes and invokes therequest, parses it, initializes and invokes the
model, takes the model response and sends it tomodel, takes the model response and sends it to
the view or presentation layerthe view or presentation layer
Controller – controller/Controller.phpController – controller/Controller.php
17. class Book {class Book {
public $title;public $title;
public $author;public $author;
public $description;public $description;
public function __construct($title, $author,public function __construct($title, $author,
$description)$description)
{{
$this->title = $title;$this->title = $title;
$this->author = $author;$this->author = $author;
$this->description = $description;$this->description = $description;
}}
}}
Model – model/Book.phpModel – model/Book.php
18.
Their sole purpose is to keep dataTheir sole purpose is to keep data
Depending on the implementation, entity objectsDepending on the implementation, entity objects
can be replaced by XML or JSON chunk of datacan be replaced by XML or JSON chunk of data
It is recommended that entities do notIt is recommended that entities do not
encapsulate any business logicencapsulate any business logic
Model - EntitiesModel - Entities
19. require_once("model/Book.php");require_once("model/Book.php");
class Model {class Model {
public function getBookList()public function getBookList()
{{
// here goes some hardcoded values to simulate// here goes some hardcoded values to simulate
the databasethe database
return array(return array(
"Jungle Book" => new Book("Jungle Book", "R."Jungle Book" => new Book("Jungle Book", "R.
Kipling", "A classic book."),Kipling", "A classic book."),
"Moonwalker" => new Book("Moonwalker", "J."Moonwalker" => new Book("Moonwalker", "J.
Walker", ""),Walker", ""),
"PHP for Dummies" => new Book("PHP for"PHP for Dummies" => new Book("PHP for
Dummies", "Some Smart Guy", "")Dummies", "Some Smart Guy", "")
););
}}
// continued...// continued...
Model – model/Model.phpModel – model/Model.php
20. public function getBook($title)public function getBook($title)
{{
// we use the previous function to get all the// we use the previous function to get all the
books and then we return the requested one.books and then we return the requested one.
// in a real life scenario this will be done// in a real life scenario this will be done
through a db select commandthrough a db select command
$allBooks = $this->getBookList();$allBooks = $this->getBookList();
return $allBooks[$title];return $allBooks[$title];
}}
}}
Model – model/Model.phpModel – model/Model.php
21.
In a real-world scenario, the model will include allIn a real-world scenario, the model will include all
the entities and the classes to persist data intothe entities and the classes to persist data into
the database, and the classes encapsulating thethe database, and the classes encapsulating the
business logicbusiness logic
ModelModel
24.
Responsible for formatting the data receivedResponsible for formatting the data received
from the model in a form accessible to the userfrom the model in a form accessible to the user
The data can come in different formats from theThe data can come in different formats from the
model: simple objects (sometimes called Valuemodel: simple objects (sometimes called Value
Objects), XML structures, JSON, etc.Objects), XML structures, JSON, etc.
ViewView
25.
The Model and View are separated, making theThe Model and View are separated, making the
application more flexibleapplication more flexible
The Model and View can be changed separately,The Model and View can be changed separately,
or replacedor replaced
Each module can be tested and debuggedEach module can be tested and debugged
separatelyseparately
AdvantagesAdvantages