model view controller (mvc)...php & mvc sumber materi : sandika galih | bayu priyambadha title...

24
Model View Controller (MVC) Pertemuan 3,4 Pemrograman Berbasis Web II Zuhar Musliyana, S.ST., M.T Universitas Ubudiyah Indonesia

Upload: others

Post on 19-Feb-2020

40 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Model View Controller (MVC)...PHP & MVC Sumber Materi : Sandika Galih | Bayu Priyambadha Title Untitled Presentation Author Unknown Creator Created Date 3/13/2019 8:15:47 AM

Model View Controller (MVC)

Pertemuan 3,4

Pemrograman Berbasis Web II Zuhar Musliyana, S.ST., M.T

Universitas Ubudiyah Indonesia

Page 2: Model View Controller (MVC)...PHP & MVC Sumber Materi : Sandika Galih | Bayu Priyambadha Title Untitled Presentation Author Unknown Creator Created Date 3/13/2019 8:15:47 AM
Page 3: Model View Controller (MVC)...PHP & MVC Sumber Materi : Sandika Galih | Bayu Priyambadha Title Untitled Presentation Author Unknown Creator Created Date 3/13/2019 8:15:47 AM
Page 4: Model View Controller (MVC)...PHP & MVC Sumber Materi : Sandika Galih | Bayu Priyambadha Title Untitled Presentation Author Unknown Creator Created Date 3/13/2019 8:15:47 AM
Page 5: Model View Controller (MVC)...PHP & MVC Sumber Materi : Sandika Galih | Bayu Priyambadha Title Untitled Presentation Author Unknown Creator Created Date 3/13/2019 8:15:47 AM
Page 6: Model View Controller (MVC)...PHP & MVC Sumber Materi : Sandika Galih | Bayu Priyambadha Title Untitled Presentation Author Unknown Creator Created Date 3/13/2019 8:15:47 AM
Page 7: Model View Controller (MVC)...PHP & MVC Sumber Materi : Sandika Galih | Bayu Priyambadha Title Untitled Presentation Author Unknown Creator Created Date 3/13/2019 8:15:47 AM
Page 8: Model View Controller (MVC)...PHP & MVC Sumber Materi : Sandika Galih | Bayu Priyambadha Title Untitled Presentation Author Unknown Creator Created Date 3/13/2019 8:15:47 AM
Page 9: Model View Controller (MVC)...PHP & MVC Sumber Materi : Sandika Galih | Bayu Priyambadha Title Untitled Presentation Author Unknown Creator Created Date 3/13/2019 8:15:47 AM
Page 10: Model View Controller (MVC)...PHP & MVC Sumber Materi : Sandika Galih | Bayu Priyambadha Title Untitled Presentation Author Unknown Creator Created Date 3/13/2019 8:15:47 AM

The model is responsible to manage

the data

The view (presentation) is

responsible to display the data

provided by the model in a specific

format

The controller handles the model

and view layers to work together

MVC

Page 11: Model View Controller (MVC)...PHP & MVC Sumber Materi : Sandika Galih | Bayu Priyambadha Title Untitled Presentation Author Unknown Creator Created Date 3/13/2019 8:15:47 AM
Page 12: Model View Controller (MVC)...PHP & MVC Sumber Materi : Sandika Galih | Bayu Priyambadha Title Untitled Presentation Author Unknown Creator Created Date 3/13/2019 8:15:47 AM
Page 13: Model View Controller (MVC)...PHP & MVC Sumber Materi : Sandika Galih | Bayu Priyambadha Title Untitled Presentation Author Unknown Creator Created Date 3/13/2019 8:15:47 AM

MVC

Page 14: Model View Controller (MVC)...PHP & MVC Sumber Materi : Sandika Galih | Bayu Priyambadha Title Untitled Presentation Author Unknown Creator Created Date 3/13/2019 8:15:47 AM
Page 15: Model View Controller (MVC)...PHP & MVC Sumber Materi : Sandika Galih | Bayu Priyambadha Title Untitled Presentation Author Unknown Creator Created Date 3/13/2019 8:15:47 AM
Page 16: Model View Controller (MVC)...PHP & MVC Sumber Materi : Sandika Galih | Bayu Priyambadha Title Untitled Presentation Author Unknown Creator Created Date 3/13/2019 8:15:47 AM

PHP & MVC

Page 17: Model View Controller (MVC)...PHP & MVC Sumber Materi : Sandika Galih | Bayu Priyambadha Title Untitled Presentation Author Unknown Creator Created Date 3/13/2019 8:15:47 AM

model/Book.php

<?php

class Book {

public $title;

public $author;

public $description;

public function __construct($title, $author,

$description)

{

$this->title = $title;

$this->author = $author;

$this->description = $description;

}

}

?>

Page 18: Model View Controller (MVC)...PHP & MVC Sumber Materi : Sandika Galih | Bayu Priyambadha Title Untitled Presentation Author Unknown Creator Created Date 3/13/2019 8:15:47 AM

model/Model.php

<?php

include_once("model/Book.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)

{

// 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];

}

}

Page 19: Model View Controller (MVC)...PHP & MVC Sumber Materi : Sandika Galih | Bayu Priyambadha Title Untitled Presentation Author Unknown Creator Created Date 3/13/2019 8:15:47 AM

view/viewbook.php

<html>

<head></head>

<body>

<?php

echo 'Title:' . $book->title . '<br/>';

echo 'Author:' . $book->author . '<br/>';

echo 'Description:' . $book->description . '<br/>';

?>

</body>

</html>

Page 20: Model View Controller (MVC)...PHP & MVC Sumber Materi : Sandika Galih | Bayu Priyambadha Title Untitled Presentation Author Unknown Creator Created Date 3/13/2019 8:15:47 AM

view/booklist.php

<html>

<head></head>

<body>

<table>

<tbody>

<tr><td>Title</td><td>Author</td><td>Description</td></tr>

</tbody>

<?php

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>';

}

?>

</table>

</body>

</html>

Page 21: Model View Controller (MVC)...PHP & MVC Sumber Materi : Sandika Galih | Bayu Priyambadha Title Untitled Presentation Author Unknown Creator Created Date 3/13/2019 8:15:47 AM

controller/Controller.php

<?php

include_once("model/Model.php");

class Controller {

public $model;

public function __construct()

{

$this->model = new Model();

}

Page 22: Model View Controller (MVC)...PHP & MVC Sumber Materi : Sandika Galih | Bayu Priyambadha Title Untitled Presentation Author Unknown Creator Created Date 3/13/2019 8:15:47 AM

index.php

<?php

// All interaction goes through the index and is

forwarded

// directly to the controller

include_once("controller/Controller.php");

$controller = new Controller();

$controller->invoke();

?>

Page 23: Model View Controller (MVC)...PHP & MVC Sumber Materi : Sandika Galih | Bayu Priyambadha Title Untitled Presentation Author Unknown Creator Created Date 3/13/2019 8:15:47 AM

PHP & MVC

Page 24: Model View Controller (MVC)...PHP & MVC Sumber Materi : Sandika Galih | Bayu Priyambadha Title Untitled Presentation Author Unknown Creator Created Date 3/13/2019 8:15:47 AM

Sumber Materi : Sandika Galih | Bayu Priyambadha