Your SlideShare is downloading. ×
PHP MVC
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

PHP MVC

3,317
views

Published on

PHP MVC …

PHP MVC
- Definition
- Sample MVC application
- Advantages

Published in: Technology

0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,317
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
133
Comments
0
Likes
4
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. PHP MVCPHP MVC Reggie Niccolo SantosReggie Niccolo Santos UP ITDCUP ITDC
  • 2.  DefinitionDefinition  Sample MVC applicationSample MVC application  AdvantagesAdvantages OutlineOutline
  • 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
  • 4. 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
  • 8. MVC Collaboration DiagramMVC Collaboration Diagram
  • 9. MVC Sequence DiagramMVC Sequence Diagram
  • 10. Sample MVC ApplicationSample MVC Application
  • 11. 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
  • 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
  • 22. <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
  • 23. <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?book='.echo '<tr><td><a href="index.php?book='. $book->title.'">'.$book->title.'</a></td><td>'.$book->title.'">'.$book->title.'</a></td><td>'. $book->author.'</td><td>'.$book-$book->author.'</td><td>'.$book- >description.'</td></tr>';>description.'</td></tr>'; }} ?>?> </tbody></table></tbody></table> </body></body> </html></html> View – view/booklist.phpView – view/booklist.php
  • 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
  • 26. http://php-html.net/tutorials/model-view-controller-in-phttp://php-html.net/tutorials/model-view-controller-in-p http://www.htmlgoodies.com/beyond/php/article.php/3http://www.htmlgoodies.com/beyond/php/article.php/3 http://www.phpro.org/tutorials/Model-View-Controller-Mhttp://www.phpro.org/tutorials/Model-View-Controller-M http://www.particletree.com/features/4-layers-of-separhttp://www.particletree.com/features/4-layers-of-separ ReferencesReferences