Upload
phamthuy
View
216
Download
0
Embed Size (px)
Citation preview
Andreas Schmidt PHP (MVC) 1/22
Fakultät für Informatik & Wirtschaftsinformatik DB & IS II - WS 2018
Model-View-Controler Paradigma
Der Inhalt dieses Foliensatzes ist nicht klausurrelevent !!!
-aber trotzdem interessant ;-)
Andreas Schmidt PHP (MVC) 2/22
Fakultät für Informatik & Wirtschaftsinformatik DB & IS II - WS 2018
Model-View-Controler paradigma (1)
GUI
OO-application logic
Object-relational-Mapping
Relational DBMS
Views
Controlers
Business logic
Andreas Schmidt PHP (MVC) 3/22
Fakultät für Informatik & Wirtschaftsinformatik DB & IS II - WS 2018
Schlagworte webbasierte Informationssysteme
• WWW-Browser
• WWW-Server
• HTTP-Protokoll
• HTML
• Javascript
• URL
• Skriptsprachen
• CGI-Programme
• PHP, ASP, JSP
• Applets
• Servlets
• Datenbanken
• ODBC
• JDBC
Andreas Schmidt PHP (MVC) 4/22
Fakultät für Informatik & Wirtschaftsinformatik DB & IS II - WS 2018
Dateisystem
Datenbanksystem
WWW-ServerWWW-Browser
WWW-Browser
PHP-Modul
Servlet-Container
CGI-Schnittstelle
Applet
Javascript
HTML-Seite
HTML-Seite
*.html
*.pl
*.asp*.php
*.class
*.gif
HTTP
HTTPODBCJDBC
JDBC
HTTP
Architektur von Webanwendungen
Andreas Schmidt PHP (MVC) 5/22
Fakultät für Informatik & Wirtschaftsinformatik DB & IS II - WS 2018
• request:http://www.iai.fzk.de:8080/PIA/members.php?id=3535&mode=short
specifies the server
specifies the concrete file on this server
protocol
specifies parameter passed to the script
URL request
Andreas Schmidt PHP (MVC) 6/22
Fakultät für Informatik & Wirtschaftsinformatik DB & IS II - WS 2018
MVC ParadigmaModel-View-Controler paradigma (2)
• Modell: Klassen, welche die Applikati-onslogik enthalten (ohne Benutzungs-schnittstelle)
• View: Visualisierung von Informationen aus dem Modell in einem oder mehre-ren Fenstern
• Controller: Ausführung der durch den Benutzer initierten Aktionen
Model
View
Controlerreading
modify
activate
interaction
3
1
2
4
call
Andreas Schmidt PHP (MVC) 7/22
Fakultät für Informatik & Wirtschaftsinformatik DB & IS II - WS 2018
navigational structure
startpage.php
new_film_mask.phplist_films.php
edit_film_mask.php
controler_remove_actor.php
controler_update_film.phpcontroler_delete_film.php
Views
Controler
controler_create_film.php
Andreas Schmidt
Fakultät für Informatik & Wirtschaftsinformatik DB & IS II - WS 2018
PHP (MVC) 8/22
view edit_film_mask.php (1)
Andreas Schmidt PHP (MVC) 9/22
Fakultät IWIDB & IS II - WS 2018
<?php
include ('OR-Film.php');
$id = $_REQUEST['id'];
if (empty($id))
die("Parameter 'id' nicht gesetzt");
PDO_Util::connect('mysql:host=localhost;dbname=film',’root’,’’);
$film = Film::getById($id);
?>
<html>
<body>
<form action="controler_update_film.php">
<input type="hidden" name="film[id]"
value="<?php echo $film->getId() ?>">
<table width="80%" border="1">
<tr>
<th>Title</th>
<td><input type="textfield" name="film[title]"
size="40"
value="<?php echo $film->getTitle() ?>">
</td>
</tr>
<tr>
<th>Year</th>
<td><input type="textfield" name="film[year]"
value="<?php echo $film->getYear() ?>">
</td>
</tr>
<tr>
<td></td>
<td align="center">
<input type="submit" value="Store">
<input type="reset" value="Reset">
</td>
</tr>
</table>
</form>
view edit_film_mask.php (2)
action
Andreas Schmidt PHP (MVC) 10/22
Fakultät IWIDB & IS II - WS 2018
controler_update_film.php
<?php
include (’OR-Film.php’);
// Parameter einlesen
//
$film = $_REQUEST[’film’];
$id = $film[’id’];
$title = $film[’title’];
$year = $film[’year’];
// Plausibilitätstests der übergebenen Parameter (wichtig !!)
//
if (! preg_match("/^[-.A-Za-z0-9@ /]+$/", $title))
die("($title) : Illegal title format");
if ($year && ($year < 1900 || $year > Date("Y"))
die("($year) : Illegal year");
// Modell modifizieren
PDO_Util::connect('mysql:host=localhost;dbname=film',’root’,’’);
$f = Film::getById($id);
$f->setYear($year);
$f->setTitle($title);
$f->update();
PDO_Util::commit();
// Browser mitteilen, welche Seite geladen werden soll
//
header("location: list_film_mask.php");
?>
Andreas Schmidt PHP (MVC) 11/22
Fakultät IWIDB & IS II - WS 2018
Zentralcontroller
• Jeder Controler muss bestimmte Aufgaben unabhängig von eigentlicher Aktion ausführen (z.B. überprüfen ob Zugriffsbe-rechtigung existiert)
• Idee: Ein Controler der alle Anfragen entgegen nimmt
• Beispiel:<?php
include (’OR-Film.php’);
include(’Action.php’);
// Parameter einlesen
//
$target_view = $_REQUEST[’target_view’];
$last_view = $_SERVER['HTTP_REFERER'];
if (is_authenticated()) {
PDO_Util::connect('mysql:host=localhost;dbname=film',’root’,’’);
list ($status, $err) = Action::handle_request($_REQUEST);
PDO_Util::commit();
if ($status==true)
View::target_view($target_view);
else
View::target_view($last_view , $err);
} else {
View::target_view(’authenticate.php’);
}
?>
sendet http-header
Andreas Schmidt PHP (MVC) 12/22
Fakultät für Informatik & Wirtschaftsinformatik DB & IS II - WS 2018
Beispiel für Action Klasse
class Action {
public static function handle_request($parameters) {
$task = $parameters['task'];
if ($task == 'create_person')
return Action::create_person($parameters);
} else if ($task == ...) {
...
} else {
die ("illegal action '$task' specified");
}
...
}
private static function create_person($parameters) {
$para_errors = check_parameters($parameters,
array('name','first_name'),
array('date_of_birth'));
...
}
...
Andreas Schmidt PHP (MVC) 13/22
Fakultät für Informatik & Wirtschaftsinformatik DB & IS II - WS 2018
Objektrelationale Beispielanwendung
• Download der Beispiele
• Browser auf:http://localhost/or-mondial-example/ richten
• Anwendung demonstriert:
• Anlegen neuer Länder
• Auflisten der bestehenden Länder
• Löschen von Ländern (aus Listen-/Einzelansicht)
Andreas Schmidt PHP (MVC) 14/22
Fakultät für Informatik & Wirtschaftsinformatik DB & IS II - WS 2018
Struktur der Anwendung
• Library-Dateien
• mondial-or.php: Datei enthält Klasse Land und Stadt
• PDO_Util.php: Klasse PDO_Util zum Verwalten einer Datenbankverbindung
• Util.php: Klasse MVC_Util zum komfortableren Entwickeln von Controllern (anzeigen aller übergebenen Parameter, Weiterleitung zur Zielseite per Click stattautomatisch)
Klasse HTML zum Darstellen von HTML Tabellen und Auswahlboxen
• Views und Controller
• landesliste.php
• land_neu.php
• land_editieren.php
• controller_land_loeschen.php
• controller_land_modifizieren.php
• controller_land_eintragen.php
Andreas Schmidt PHP (MVC) 15/22
Fakultät für Informatik & Wirtschaftsinformatik DB & IS II - WS 2018
Debugausgaben im Controller (Klasse Util)
<?
include ('mondial-or.php');
include ('Util.php');
$code = $_REQUEST[code];
if (! isset($code))
die($_SERVER[’PHP_SELF’]." fehlende
Parameter: \$code: $code");
MVC_Util::debug(1); // 1 = debugmode
$dsn = "mysql:host=localhost;dbname=mondial";
$db = PDO_Util::connect($dsn);
$land = Land::get($code);
$land->delete();
PDO_Util::commit();
MVC_Util::debug_info('jetzt ists weg');
MVC_Util::goto_page("landesliste.php");
?>
<?
include ('mondial-or.php');
include ('Util.php');
$code = $_REQUEST[code];
if (! isset($code))
die($_SERVER[’PHP_SELF’]." fehlende
Parameter: \$code: $code");
$dsn = "mysql:host=localhost;dbname=mondial";
$db = PDO_Util::connect($dsn);
$land = Land::get($code);
$land->delete();
PDO_Util::commit();
header("location: landesliste.php");
?>
Andreas Schmidt PHP (MVC) 16/22
Fakultät für Informatik & Wirtschaftsinformatik DB & IS II - WS 2018
Debugausgaben des Controllers
Andreas Schmidt PHP (MVC) 17/22
Fakultät für Informatik & Wirtschaftsinformatik DB & IS II - WS 2018
Klasse HTML
• Klasse mit statischen Methoden zur Darstellung von HTML-Elementen
• Methode table
• Aufruf:
HTML::table($objekt_liste, $spaltenarray)
• Beispiel:
<?php HTML::table($film->get_actors(),
array(’$obj->get_prename()’,
’$obj->get_surname()’)
);
?>
Andreas Schmidt PHP (MVC) 18/22
Fakultät für Informatik & Wirtschaftsinformatik DB & IS II - WS 2018
Klasse HTML
• Methode selectbox
• Aufruf:
HTML::selectbox( $name, $objekt_liste, $key_value_array,
$default="", $onclick="");
• Beispiel:<?= HTML::selectbox(’regisseur’,
Person::query(),
array(’$obj->getId()’,
’$obj->get_name()." ".$obj->get_prename()’),
$regisseur_id);
?>
Andreas Schmidt PHP (MVC) 19/22
Fakultät für Informatik & Wirtschaftsinformatik DB & IS II - WS 2018
sonstiges
• Hidden Fields (HTML):
• Zweck: Übergabe von Werten an nächste Seite
• Beispiel:<form action="controller_land_modifizieren.php">
<table border="1">
<tr>
<th>Landeskürzel</th>
<td><?= $land->getId() ?></td>
<input type="hidden" name="land_l_id"
value="<?= $land->getId() ?>">
</tr>
...
• Image als Button (HTML)<a href="controller_land_loeschen.php?land_l_id=<?=
$land->getId() ?>">
<img src="trashcan.gif" border="0" width="30">
</a>
Andreas Schmidt PHP (MVC) 20/22
Fakultät für Informatik & Wirtschaftsinformatik DB & IS II - WS 2018
nützliches ...
• uniqid ($prefix)-- Erzeugt eine eindeutige ID
• rawurlencode($str) -- URL-Kodierung nach RFC 1738Bsp.: <a href="show_lesson.php?text="<?= rawurlencode(’DB&IS II’)?>">Databases & Information Systems II</a>
• htmlentities($string): Konvertiert bestimmte Zeichen (<, >, &, ...) nach HTML-Entities (<, > , &, ...)
Andreas Schmidt PHP (MVC) 21/22
Fakultät für Informatik & Wirtschaftsinformatik DB & IS II - WS 2018
Tipps
• HTML-Fehler:z.B. keine Ausgabe wegen falsche geschachtelter oder fehlender Tags
• Kontrolle:- Im Browserfenster „View Source Code“ bzw. „Quellcode anzeigen“ ausführen
(mittels rechter Maustaste)- Programm außerhalb des Webservers ausführen:
Andreas Schmidt PHP (MVC) 22/22
Fakultät für Informatik & Wirtschaftsinformatik DB & IS II - WS 2018
Übungsaufgabe (nicht klausurrelevant)
• Erstelle, aufbauend auf dem Übungsblatt 2 (Schiffcharter) eine Übersichtsseite, welche alle Häfen auflistet.
• Erstelle zwei Views samt zugehörigen Controllern, welche das Anlegen von Schif-fen und Häfen erlaubt.
• Erstelle weiterhin eine Seite, welche die Details eines Hafens anzeigt. Diese Seite soll durch einen Link mit der Übersichtsseite aller Häfen verknüpft sein.
• Erweitere die Hafen Detailseite, so dass einzelne Felder modifiziert werden kön-nen und dass Schiffe dem Hafen als Heimathafen zugewiesen werden können.
Hinweis: Erweitere gegebenenfalls die Appplikationslogik in den Dateien Schiff.php und Hafen.php