PHP MVC - home.hs-karlsruhe.desech0004/vorlesungen/20150509_MVC.pdf · Fakultät für Informatik &...
Transcript of PHP MVC - home.hs-karlsruhe.desech0004/vorlesungen/20150509_MVC.pdf · Fakultät für Informatik &...
Fakultät für Informatik & Wirtschaftsinformatik
DB & IS II – SS 2015
Dr. Christian Senger
PHP MVC
PHP MVC 1 von 21
Fakultät für Informatik & Wirtschaftsinformatik
DB & IS II – SS 2015
Dr. Christian Senger
Model View Control
GUI
OO-application logic
Object-relational-Mapping
Relational DBMS
Views
Controlers
Business logic
PHP MVC 2 von 21
Fakultät für Informatik & Wirtschaftsinformatik
DB & IS II – SS 2015
Dr. Christian Senger
Webbasierte Informationssysteme
• WWW-Browser
• WWW-Server
• HTTP-Protokoll
• HTML
• Javascript
• URL
• Skriptsprachen
• CGI-Programme
• PHP, ASP, JSP
• Applets
• Servlets
• Datenbanken
• ODBC
• JDBC
PHP MVC 3 von 21
Fakultät für Informatik & Wirtschaftsinformatik
DB & IS II – SS 2015
Dr. Christian Senger
WWW-Server
Architektur von Webanwendungen
Dateisystem
DBMS
WWW-Browser
HTML-Seite
Javascript
WWW-Browser
HTML-Seite
Applet
*.html *.gif
*.php
*.asp
*.class *.pl
http
PHP-Modul
Servlet-Container
CGI-Schnittstelle
http
http
jdbc
jdbc odbc
PHP MVC 4 von 21
Fakultät für Informatik & Wirtschaftsinformatik
DB & IS II – SS 2015
Dr. Christian Senger
URL-Requests
http://www.couchsurfing.de:8080/teilnehmer.php?id=3535&anzeige=adresse
Protokoll
Server
Port
genaue Datei
Parameter, die dem Server übergeben werden
PHP MVC 5 von 21
Fakultät für Informatik & Wirtschaftsinformatik
DB & IS II – SS 2015
Dr. Christian Senger
Model-View-Control
• Modell: Klassen, welche die
Applikationslogik enthalten
(ohne Benutzungsschnittstelle)
• View: Visualisierung von
Informationen aus dem Modell
in einem oder mehreren
Fenstern
• Controller: Ausführung der
durch den Benutzer initiierten
Aktionen
Model
View
Controller
1. reading
3. modify
2. activate
4. call
interaction
PHP MVC 6 von 21
Fakultät für Informatik & Wirtschaftsinformatik
DB & IS II – SS 2015
Dr. Christian Senger
Struktur u. Navigation, Bsp. FilmDB
startpage.php
new_film_mask.php list_films.php
edit_film_mask.php
controler_remove_actor.php
controler_update_film.php controler_delete_film.php
Views
Controler
controler_create_film.php
PHP MVC 7 von 21
Fakultät für Informatik & Wirtschaftsinformatik
DB & IS II – SS 2015
Dr. Christian Senger
View edit_film_mask.php
PHP MVC 8 von 21
Fakultät für Informatik & Wirtschaftsinformatik
DB & IS II – SS 2015
Dr. Christian Senger
View edit_film_mask.php
<?php
include ('OR-Film.php');
$id = $_REQUEST['id'];
if (empty($id)) die("Parameter 'id' nicht gesetzt");
MDB2_Util::connect('mysql://root:@localhost/film');
$film = Film::getById($id);
?>
<html>
<body>
…
PHP MVC 9 von 21
Fakultät für Informatik & Wirtschaftsinformatik
DB & IS II – SS 2015
Dr. Christian Senger
View edit_film_mask.php
<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]"
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 align="center">
<input type="submit" value="Store">
<input type="reset" value="Reset">
</td></tr>
</table>
</form>
PHP MVC 10 von 21
Fakultät für Informatik & Wirtschaftsinformatik
DB & IS II – SS 2015
Dr. Christian Senger
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
if (! preg_match(
"/^[-.A-Za-z0-9@ /]+$/",
$title)
)
die("($title) : Illegal title format");
if (
$year < 1900 || $year > Date("Y")
)
die("($year) : Illegal year");
PHP MVC 11 von 21
Fakultät für Informatik & Wirtschaftsinformatik
DB & IS II – SS 2015
Dr. Christian Senger
controler_update_film.php
// Modell modifizieren
MDB2_Util::connect(
'mysql://root:@localhost/film');
$f = Film::getById($id);
$f->setYear($year);
$f->setTitle($title);
$f->update();
MDB2_Util::close();
// Browser mitteilen, welche Seite
geladen werden soll
//
header("location: list_film_mask.php");
?>
PHP MVC 12 von 21
Fakultät für Informatik & Wirtschaftsinformatik
DB & IS II – SS 2015
Dr. Christian Senger
Zentralcontroller
// Jeder Controller muss bestimmte Aufgaben unabhängig von eigentlicher Aktion
// ausführen (z.B. überprüfen ob Zugriffsberechtigung existiert)
// Idee: Ein Controler der alle Anfragen entgegennimmt
<?php
include (’OR-Film.php’);
include(’Action.php’);
// Parameter einlesen
//
$target_view = $_REQUEST[’target_view’];
$last_view = $_SERVER['HTTP_REFERER'];
if (is_authenticated()) {
MDB2_Util::connect('mysql://root:@localhost/film');
list ($status, $err) = Action::handle_request($_REQUEST);
MDB2_Util::close();
if ($status) View::target_view($target_view);
else View::target_view($last_view , $err);
} else View::target_view(’authenticate.php’);
?>
PHP MVC 13 von 21
Fakultät für Informatik & Wirtschaftsinformatik
DB & IS II – SS 2015
Dr. Christian Senger
Bsp. Klasse Action
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'));
...
}
...
PHP MVC 14 von 21
Fakultät für Informatik & Wirtschaftsinformatik
DB & IS II – SS 2015
Dr. Christian Senger
Objektrelationale Beispielanwendung
• Mondial einrichten, Download der
Beispiele
http://www.home.hs-
karlsruhe.de/~sech0004/
Skripte/Daten
mondial_db.zip, mondial-or.zip
• Tutorium
• Anwendung demonstriert:
– Anlegen neuer Länder
– Auflisten der bestehenden
Länder
– Löschen von Ländern (aus
Listen-/Einzelansicht
PHP MVC 15 von 21
Fakultät für Informatik & Wirtschaftsinformatik
DB & IS II – SS 2015
Dr. Christian Senger
Struktur der Anwendung
• Library-Dateien
– mondial-or.php: Datei enthält Klasse Land und Stadt
– MDB2_Util.php: Klasse MDB2_Util zum Verwalten einer Datenbankverbindung
– Util.php: Klasse MVC_Util zum komfortableren Entwickeln von Controllern (anzeigen aller übergebenen Parameter, Weiterleitung zur Zielseite per Click statt automatisch), 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
PHP MVC 16 von 21
Fakultät für Informatik & Wirtschaftsinformatik
DB & IS II – SS 2015
Dr. Christian Senger
Debugausgaben im Controller (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://root:@localhost/mondial";
$db = MDB2_Util::connect($dsn);
$land = Land::get($code);
$land->delete();
MVC_Util::debug_info('jetzt ists weg');
MVC_Util::goto_page("landesliste.php");
?>
PHP MVC 17 von 21
Fakultät für Informatik & Wirtschaftsinformatik
DB & IS II – SS 2015
Dr. Christian Senger
Debugausgaben im Controller (Util)
PHP MVC 18 von 21
Fakultät für Informatik & Wirtschaftsinformatik
DB & IS II – SS 2015
Dr. Christian Senger
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()’)
);
?>
PHP MVC 19 von 21
Fakultät für Informatik & Wirtschaftsinformatik
DB & IS II – SS 2015
Dr. Christian Senger
Klasse HMTL
• 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);
?>
PHP MVC 20 von 21
Fakultät für Informatik & Wirtschaftsinformatik
DB & IS II – SS 2015
Dr. Christian Senger
Sonstiges
Hidden Fields (HTML): Ü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>
PHP MVC 21 von 21