This commit is contained in:
Harald Mueller 2022-03-03 14:13:05 +01:00
parent 984b54f217
commit 1d1b9ce84a
60 changed files with 115 additions and 2 deletions

View File

@ -0,0 +1 @@
Source code for Model View Controller Tutorial in PHP: http://php-html.net/tutorials/model-view-controller-in-php/

View File

@ -0,0 +1,30 @@
<?php
include_once("model/Model.php");
class Controller {
public $model;
public function __construct()
{
$this->model = new Model();
}
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';
}
}
}
?>

View File

@ -0,0 +1,7 @@
<?php
include_once("controller/Controller.php");
$controller = new Controller();
$controller->invoke();
?>

View File

@ -0,0 +1,16 @@
<?php
class Book {
public $title;
public $author;
public $description;
public function __construct($title, $author, $description)
{
$this->title = $title;
$this->author = $author;
$this->description = $description;
}
}
?>

View File

@ -0,0 +1,27 @@
<?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 db select command
$allBooks = $this->getBookList();
return $allBooks[$title];
}
}
?>

View File

@ -0,0 +1,19 @@
<html>
<head></head>
<body>
<table>
<tr><td>Title</td><td>Author</td><td>Description</td></tr>
<?php
foreach ($books as $title => $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>

View File

@ -0,0 +1,15 @@
<html>
<head></head>
<body>
<?php
echo 'Title:' . $book->title . '<br/>';
echo 'Author:' . $book->author . '<br/>';
echo 'Description:' . $book->description . '<br/>';
?>
</body>
</html>

View File

@ -1,2 +0,0 @@
PHP Rapid
https://www.rapidphpeditor.com/de/?gclid=Cj0KCQiAkZHTBRCBARIsAMbXLhF6uOJ-qY-cPvGUF4Qp44wuAacVhmVoq6t9yCsgqmD3RSp7XhdML74aAtflEALw_wcB