PHP MVC
-
Upload
reggie-niccolo-santos -
Category
Technology
-
view
3.417 -
download
3
description
Transcript of PHP MVC
PHP MVCPHP MVCReggie Niccolo SantosReggie Niccolo Santos
UP ITDCUP ITDC
DefinitionDefinition
Sample MVC applicationSample MVC application
AdvantagesAdvantages
OutlineOutline
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
MVCMVC
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
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)
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
MVC Collaboration DiagramMVC Collaboration Diagram
MVC Sequence DiagramMVC Sequence Diagram
Sample MVC ApplicationSample MVC Application
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
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
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
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
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
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
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
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
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
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
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
<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
<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
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
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
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