PHP MVC

26
PHP MVC PHP MVC Reggie Niccolo Santos Reggie Niccolo Santos UP ITDC UP ITDC

description

PHP MVC - Definition - Sample MVC application - Advantages

Transcript of PHP MVC

Page 1: PHP MVC

PHP MVCPHP MVCReggie Niccolo SantosReggie Niccolo Santos

UP ITDCUP ITDC

Page 2: PHP MVC

DefinitionDefinition

Sample MVC applicationSample MVC application

AdvantagesAdvantages

OutlineOutline

Page 3: PHP MVC

Model-View-ControllerModel-View-Controller

Most used architectural pattern for today's Most used architectural pattern for today's web applicationsweb applications

Originally described in terms of a design Originally described in terms of a design pattern for use with Smalltalk by Trygyve pattern for use with Smalltalk by Trygyve Reenskaug in 1979Reenskaug in 1979

The paper was published under the title The paper was published under the title ”Applications Programming in Smalltalk-”Applications Programming in Smalltalk-80: How to use Model-View-Controller”80: How to use Model-View-Controller”

MVCMVC

Page 4: PHP MVC

MVCMVC

Page 5: PHP MVC

Responsible for managing the dataResponsible for managing the data

Stores and retrieves entities used by an Stores and retrieves entities used by an application, usually from a database but application, usually from a database but can also include invocations of external web can also include invocations of external web services or APIsservices or APIs

Contains the logic implemented by the Contains the logic implemented by the application (business logic)application (business logic)

ModelModel

Page 6: PHP MVC

Responsible to display the data provided Responsible to display the data provided by the model in a specific formatby the model in a specific format

View (Presentation)View (Presentation)

Page 7: PHP MVC

Handles the model and view layers to Handles the model and view layers to work togetherwork together

Receives a request from the client, Receives a request from the client, invokes the model to perform the requested invokes the model to perform the requested operations and sends the data to the viewoperations and sends the data to the view

ControllerController

Page 8: PHP MVC

MVC Collaboration DiagramMVC Collaboration Diagram

Page 9: PHP MVC

MVC Sequence DiagramMVC Sequence Diagram

Page 10: PHP MVC

Sample MVC ApplicationSample MVC Application

Page 11: PHP MVC

require_once("controller/Controller.php");require_once("controller/Controller.php");

$controller = new Controller();$controller = new Controller();$controller->invoke();$controller->invoke();

Entry point - index.phpEntry point - index.php

Page 12: PHP MVC

The application entry point will be The application entry point will be index.phpindex.php

The index.php file will delegate all the The index.php file will delegate all the requests to the controllerrequests to the controller

Entry point - index.phpEntry point - index.php

Page 13: PHP MVC

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/Controller.phpcontroller/Controller.php

Page 14: PHP MVC

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/Controller.phpcontroller/Controller.php

Page 15: PHP MVC

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/Controller.phpcontroller/Controller.php

Page 16: PHP MVC

The controller is the first layer which takes The controller is the first layer which takes a request, parses it, initializes and invokes a request, parses it, initializes and invokes the model, takes the model response and the model, takes the model response and sends it to the view or presentation layersends it to the view or presentation layer

Controller – Controller – controller/Controller.phpcontroller/Controller.php

Page 17: PHP MVC

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

Page 18: PHP MVC

Their sole purpose is to keep dataTheir sole purpose is to keep data

Depending on the implementation, entity Depending on the implementation, entity objects can be replaced by XML or JSON objects can be replaced by XML or JSON chunk of datachunk of data

It is recommended that entities do not It is recommended that entities do not encapsulate any business logicencapsulate any business logic

Model - EntitiesModel - Entities

Page 19: PHP MVC

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 // here goes some hardcoded values to simulate the databasesimulate the database

return array(return array("Jungle Book" => new Book("Jungle Book", "Jungle Book" => new Book("Jungle Book",

"R. Kipling", "A classic book."),"R. Kipling", "A classic book."),"Moonwalker" => new Book("Moonwalker", "Moonwalker" => new Book("Moonwalker",

"J. Walker", ""),"J. 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

Page 20: PHP MVC

public function getBook($title)public function getBook($title){{

// we use the previous function to get all // we use the previous function to get all the books and then we return the requested one.the 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

Page 21: PHP MVC

In a real-world scenario, the model will In a real-world scenario, the model will include all the entities and the classes to include all the entities and the classes to persist data into the database, and the persist data into the database, and the classes encapsulating the business logicclasses encapsulating the business logic

ModelModel

Page 22: PHP MVC

<html><html> <head></head><head></head> <body><body> <?php <?php

echo 'Title:' . $book->title . '<br/>';echo 'Title:' . $book->title . '<br/>';echo 'Author:' . $book->author . '<br/>';echo 'Author:' . $book->author . '<br/>';echo 'Description:' . $book->description . echo 'Description:' . $book->description .

'<br/>';'<br/>'; ?>?> </body></body></html></html>

View – view/viewbook.phpView – view/viewbook.php

Page 23: PHP MVC

<html><html> <head></head><head></head> <body><body> <table><tbody><table><tbody> <tr><td>Title</td><td>Author</td><tr><td>Title</td><td>Author</td> <td>Description</td></tr><td>Description</td></tr>

<?php <?php foreach ($books as $title => $book)foreach ($books as $title => $book){{

echo '<tr><td><a href="index.php?echo '<tr><td><a href="index.php?book='.$book->title.'">'.$book->title.'</a></td><td>'.book='.$book->title.'">'.$book->title.'</a></td><td>'.$book->author.'</td><td>'.$book->description.'</td></$book->author.'</td><td>'.$book->description.'</td></tr>';tr>';

}}?>?>

</tbody></table></tbody></table> </body></body></html></html>

View – view/booklist.phpView – view/booklist.php

Page 24: PHP MVC

Responsible for formatting the data Responsible for formatting the data received from the model in a form received from the model in a form accessible to the useraccessible to the user

The data can come in different formats The data can come in different formats from the model: simple objects from the model: simple objects (sometimes called Value Objects), XML (sometimes called Value Objects), XML structures, JSON, etc.structures, JSON, etc.

ViewView

Page 25: PHP MVC

The Model and View are separated, making The Model and View are separated, making the application more flexiblethe application more flexible

The Model and View can be changed The Model and View can be changed separately, or replacedseparately, or replaced

Each module can be tested and debugged Each module can be tested and debugged separatelyseparately

AdvantagesAdvantages

Page 26: PHP MVC

http://php-html.net/tutorials/model-view-cohttp://php-html.net/tutorials/model-view-controller-in-php/ntroller-in-php/http://www.htmlgoodies.com/beyond/php/ahttp://www.htmlgoodies.com/beyond/php/article.php/3912211rticle.php/3912211

http://www.phpro.org/tutorials/Model-View-http://www.phpro.org/tutorials/Model-View-Controller-MVC.htmlController-MVC.html

http://www.particletree.com/features/4-layhttp://www.particletree.com/features/4-layers-of-separation/ers-of-separation/

ReferencesReferences