PHP MVC PHP MVC 1 von 21
Model View Control Views Controlers Business logic GUI OO-application logic Object-relational-Mapping Relational DBMS PHP MVC 2 von 21
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
Architektur von Webanwendungen WWW-Browser HTML-Seite http WWW-Server Dateisystem *.html *.gif Javascript PHP-Modul *.php *.asp WWW-Browser http Servlet-Container *.class *.pl HTML-Seite Applet http CGI-Schnittstelle jdbc DBMS odbc jdbc PHP MVC 4 von 21
URL-Requests http://www.couchsurfing.de:8080/teilnehmer.php?id=3535&anzeige=adresse Protokoll genaue Datei Server Port Parameter, die dem Server übergeben werden PHP MVC 5 von 21
Modell: Klassen, welche die Applikationslogik enthalten (ohne Benutzungsschnittstelle) Fakultät für Informatik & Wirtschaftsinformatik Model-View-Control View: Visualisierung von Informationen aus dem Modell in einem oder mehreren Fenstern View interaction 4. call 2. activate Controller Controller: Ausführung der durch den Benutzer initiierten Aktionen 1. reading Model 3. modify PHP MVC 6 von 21
Struktur u. Navigation, Bsp. FilmDB startpage.php Views Controler list_films.php new_film_mask.php edit_film_mask.php controler_delete_film.php controler_update_film.php controler_remove_actor.php controler_create_film.php PHP MVC 7 von 21
View edit_film_mask.php PHP MVC 8 von 21
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
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
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
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(); // Browser mitteilen, welche Seite geladen werden soll // header("location: list_film_mask.php");?> MDB2_Util::close(); PHP MVC 12 von 21
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
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
Objektrelationale Beispielanwendung Mondial einrichten, Download der Beispiele http://www.home.hskarlsruhe.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
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
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
Debugausgaben im Controller (Util) PHP MVC 18 von 21
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
Methode selectbox, Aufruf: Fakultät für Informatik & Wirtschaftsinformatik Klasse HMTL 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
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