Pemrograman Web Model View Controller Adam Hendra Brata
Pokok Bahasan Pengenalan MVC Model View Controller Sistem Komunikasi MVC Kode Program MVC
Pengenalan Model View Controller
Pengenalan Model View Controller Model-View-Controller atau MVC adalah sebuah metode untuk membuat sebuah aplikasi dengan memisahkan data (Model) dari tampilan (View) dan cara bagaimana memprosesnya (Controller) MVC memisahkan pengembangan aplikasi berdasarkan komponen utama yang membangun sebuah aplikasi seperti manipulasi data, antarmuka pengguna, dan bagian yang menjadi kontrol dalam sebuah aplikasi. Konsep MVC adalah konsep pemisahan antara logic dengan tampilan dan database
Pengenalan Model View Controller Di PHP, kita dapat mengimplementasikan konsep MVC dengan membangun sendiri sebuah framework MVC berbasis OOP Selain kita bisa mengimplementasikan konsep MVC secara from scratch, terdapat banyak framework MVC yang siap digunakan dan relatif mudah dalam penggunaannya seperti, CodeIgniter, CakePHP, Zend Framework, Symfony dan lain - lain
Pola MVC memiliki layer yang disebut dengan model yang merepresentasikan data yang digunakan oleh aplikasi sebagaimana proses bisnis yang diasosiasikan terhadapnya. Dengan memilahnya sebagai bagian terpisah, seperti penampungan data (basis data), persistence, serta proses manipulasinya. Model
Layer ini mengandung keseluruhan detail dari implementasi user interface. Disini, komponen grafis menyediakan representasi proses internal aplikasi dan menuntun alur interaksi user terhadap aplikasi. Tidak ada layer lain yang berinteraksi dengan user, hanya view. View
Controller Terakhir, arsitektur MVC memiliki layer controller. Layer ini menyediakan detail alur program dan transisi layer, dan juga bertanggungjawab akan penampungan events yang dibuat oleh user dari view dan melakukan update terhadap komponen model menggunakan data yang dimasukkan oleh user.
Sistem Komunikasi MVC HTTP Response Control ler Model View DB HTTP Request
Sequence Diagram MVC
Kode Program Model View Controller
MVC Code Structure Sebagai contoh sederhana dalam slide ini akan digunakan model statis atau model yang tidak menggunakan data dari database
<?php class Book { public $title; public $author; public $description; model/book.php?> public function construct($title, $author, $description) { $this->title = $title; $this->author = $author; $this->description = $description;
<?php include_once("model/book.php"); model/model.php class Model { public function getbooklist() { // here goes some hardcoded values to simulate the database return array( "Jungle Book" => new Book("Jungle Book", "R. Kipling", "A classic book."), "Moonwalker" => new Book("Moonwalker", "J. Walker", ""), "PHP for Dummies" => new Book("PHP for Dummies", "Some Smart Guy", "") );
public function getbook($title)?> { model/model.php // we use the previous function to get all the books // and then we return the requested one. // in a real life scenario this will be done through a database select command $allbooks = $this-> getbooklist(); return $allbooks[$title];
<html> <head></head> <body> <?php view/viewbook.php echo 'Title:'. $book-> title. '<br/>'; echo 'Author:'.$book-> author. '<br/>'; echo 'Description:'. $book->description. '<br/>';?> </body> </html>
view/booklist.php <html> <head></head> <body> <table> <tbody> <tr><td>title</td><td>author</td><td>description</td> </tr> </body> </html> </tbody> <?php?> </table> foreach ($books as $book){ echo '<tr><td><a href="index.php?book='. $book->title. '">'. $book->title. '</a></td><td>'. $book->author. '</td><td>'.$book-> description. '</td></tr>';
controller/controller.php <?php include_once("model/model.php"); class Controller { public $model; public function construct() { $this->model = new Model();
controller/controller.php public function invoke() {?> if (!isset($_get['book'])) { // no special book is requested, we'll show a list of all available books $books = $this->model->getbooklist(); include 'view/booklist.php'; else { // show the requested book $book = $this->model->getbook($_get['book']); include 'view/viewbook.php';
index.php <?php // All interaction goes through the index and is forwarded // directly to the controller include_once("controller/controlle r.php"); $controller = new Controller(); $controller->invoke();?>
Terimakasih dan Semoga Bermanfaat ^^