Dynamische Websites. Week 7



Similar documents
OGH: : 11g in de praktijk

Tuesday, February 26, Unit testen in de praktijk

Web development... the server side (of the force)

Uniface en PostgreSQL, de eerste ervaringen

Table of contents. 2. Technical details Protocols used Messaging security Encoding Input and output...

A Tour of Silex and Symfony Components. Robert

Technical Specification ideal

GMP-Z Annex 15: Kwalificatie en validatie

Maximizer Synergy. BE Houwaartstraat 200/1 BE 3270 Scherpenheuvel. Tel: Fax:

Shopper Marketing Model: case Chocomel Hot. Eric van Blanken 20th October 2009

Specification by Example (methoden, technieken en tools) Remco Snelders Product owner & Business analyst

Server-side: PHP and MySQL (continued)

The information in this report is confidential. So keep this report in a safe place!

Web Development using PHP (WD_PHP) Duration 1.5 months

IP-NBM. Copyright Capgemini All Rights Reserved

GMP-Z Hoofdstuk 4 Documentatie. Inleiding

Management control in creative firms

Hoorcollege marketing 5 de uitgebreide marketingmix. Sunday, December 9, 12

1. Building Testing Environment

Certified PHP/MySQL Web Developer Course

How To Let A Lecturer Know If Someone Is At A Lecture Or If They Are At A Guesthouse

Proprietary Kroll Ontrack. Data recovery Data management Electronic Evidence

Better Training for Safer Food Initiative

Examen Software Engineering /09/2011

Site Store Pro. INSTALLATION GUIDE WPCartPro Wordpress Plugin Version

VIDEO CREATIVE IN A DIGITAL WORLD Digital analytics afternoon. Hugo.schurink@millwardbrown.com emmy.brand@millwardbrown.com

Welkom! Copyright 2014 Oracle and/or its affiliates. All rights reserved.

CO-BRANDING RICHTLIJNEN

IC Rating NPSP Composieten BV. 9 juni 2010 Variopool

Querying Databases Using the DB Query and JDBC Query Nodes

Example for Using the PrestaShop Web Service : CRUD

Apache & Virtual Hosts & mod_rewrite

Online shopping store

Application note: Connecting the to a Database

7 Web Databases. Access to Web Databases: Servlets, Applets. Java Server Pages PHP, PEAR. Languages: Java, PHP, Python,...

Real SQL Programming 1

DIPLOMA IN WEBDEVELOPMENT

By : Ashish Modi. CRUD USING PHP (Create, Read, Update and Delete on Database) Create Database and Table using following Sql Syntax.

Zend Framework Database Access

PHP Authentication Schemes

Dutch Mortgage Market Pricing On the NMa report. Marco Haan University of Groningen November 18, 2011

Role Based Access Control. Using PHP Sessions

Research Report. Ingelien Poutsma Marnienke van der Maal Sabina Idler

Vermenigvuldig en Afdeling (Intermediêre fase)

Merchant API for PHP libcurl Implementation Guide to the PHP libcurl Examples for Apache Web Server Version 1.0.5

Windows Azure Push Notifications

Sample test Secretaries/administrative. Secretarial Staff Administrative Staff

Implementeren van HL7v3 Web Services

INSTALLING, CONFIGURING, AND DEVELOPING WITH XAMPP

Advanced Object Oriented Database access using PDO. Marcus Börger

Writing Scripts with PHP s PEAR DB Module

Load Balancing Lync Jaap Wesselius

Hello friends, This is Aaditya Purani and i will show you how to Bypass PHP LFI(Local File Inclusion)

Verticale tuin maken van een pallet

ONLINE BACKUP S e r v i c e s USER MANUAL. Eljes Online Backup Management Console 3.8

total dutch speak Dutch instantly no books no writing absolute confi dence

Network Assessment Client Risk Report Demo

Simulating Variable Message Signs Influencing dynamic route choice in microsimulation

Facebook Twitter YouTube Google Plus Website

Breaking Web Applications in Shared Hosting Environments. Nick Nikiforakis Katholieke Universiteit Leuven

Cisco Small Business Fast Track Pricing Initiative

Market Intelligence & Research Services. CRM Trends Overview. MarketCap International BV Januari 2011

Server side scripting and databases

TIMETABLE ADMINISTRATOR S MANUAL

Using IRDB in a Dot Net Project

How To Get A Ticket To The Brits Engels

Create e-commerce website Opencart. Prepared by : Reth Chantharoth Facebook : rtharoth@yahoo.

Written by: Johan Strand, Reviewed by: Chafic Nassif, Date: Getting an ipath server running on Linux

Cambridge International Examinations Cambridge International General Certificate of Secondary Education

Joomla 1.0 Extension Development Training. Learning to program for Joomla

HOW TO SETUP AN APACHE WEB SERVER AND INTEGRATE COLDFUSION

MAYORGAME (BURGEMEESTERGAME)

Qualtrics Single Sign-On Specification

E-Commerce met Microsoft

How To Design A 3D Model In A Computer Program

Visie op Hosted Services: Cloud Computing. Michel N guettia Business Lead Server

MIMOA. UID MIMOA Demo 0.8 Version: UID_v0.83.vsd. Versie: UID_v0.83.vsd. UID MIMOA Launch. Matthijs Collard 1. Elements 2. Elements 2 3.

Self-test SQL Workshop

INSEAD ALUMNI ASSOCIATION THE NETHERLANDS EVENT CALENDAR

Special Interest Group Oracle WebCenter

Web Development Guide. Information Systems

SURFfederatie - edugain. Opt-in Metadata Management for a Hub & Spoke Federation

Web applications. Web security: web basics. HTTP requests. URLs. GET request. Myrto Arapinis School of Informatics University of Edinburgh

Individual project 2010: Kick- off. 13 April 2010

Log Analyzer Reference

CSCI110 Exercise 4: Database - MySQL

pset 7: C$50 Finance Zamyla Chan

Bld. du Roi Albert II, 27, B 1030 BRUSSELS Tel Fax Secure file upload in PHP web applications

Transcription:

Dynamische Websites Week 7

Lesschema Herhaling Front Controller AGENDA

LESSCHEMA Lesweek 7: Framework - Front Controller Lesweek 8: Framework - 2 Step Design Lesweek 9: Framework - Forms en... Lesweek 10: Framework - Vragen Lesweek 11: Extra topics Lesweek 12: Herhaling belangrijkste concepten Lesweek 13: Vragen en uitleg examen

Lesschema Herhaling Front Controller AGENDA

DB GEGEVENS <?php date_default_timezone_set('europe/brussels'); // local_u0034562 via webontwerp => 'host' => 'localhost' // u0034562 via mamp, wamp, xampp => 'host' => 'gegevensbanken.khleuven.be' require_once('../../../../.db_password.php'); $db_config = array( 'driver' => 'pgsql', 'username' => $username, 'password' => $password, 'schema' => 'public', // dit moet je veranderen naar je eigen schema 'dsn' => array( 'host' => 'gegevensbanken.khleuven.be', 'dbname' => 'webontwerp', // dit moet je veranderen naar db van je reeks 'port' => '51314', ) );

.DB_PASSWORD.PHP.db_password.php <?php $dbuser = "local_u0034562"; $dbpassword = "Qpnu7dj9YOio";?> hidden nu reeds op server

AGENDA Lesschema Herhaling Front Controller

FRONT CONTROLLER = is een dispatching controller naar wie alle requests worden gedaan alles gaat via index.php controllers, actions en parameters worden hieraan meegegeven https://en.wikipedia.org/wiki/ Front_Controller_pattern

FRONT CONTROLLER VIA $_GET EN IF Voorbeelden http://example.com/index.php?action=new http://example.com/index.php?action=home Zie dynweb2013examples/theorie07/ frontcontroller_stap01

VOORBEELD HOME http://example.com/index.php?action=home

INDEX.PHP <?php require_once('controller.php'); $controller = new Controller(); $controller->run(); Code Onveranderd

CONTROLLER.PHP <?php class Controller{ private $_vehicles; private $_vehiclemapper;... public function run(){ $nextpage = 'index.php'; if ($this->_action == 'register') { $nextpage = $this->register(); elseif ($this->_action == 'new') { $nextpage = $this->newregistration(); elseif ($this->_action == 'home') { $nextpage = $this->home(); require_once($nextpage);... Code Onveranderd

CONTROLLER.PHP...?> private function home() { return 'home.php'; Code Onveranderd

HOME.PHP <body> <h1>welkom!</h1> <a href="index.php?action=new">registreer voertuig</a> </body> Code Onveranderd

FRONT CONTROLLER VIA $_GET EN IF Uitbreiding als we een detail pagina willen toevoegen waarmee we alle informatie van dat specifiek vehicle willen tonen

FRONT CONTROLLER VIA $_GET EN IF Problemen? run methode wordt veel te lang voldoet niet aan het Open-Closed principe nieuwe functionaliteit kunnen toevoegen door zo weinig mogelijk bestaande code aan te passen

FRONT CONTROLLER OO VIA $_GET Controller if-then-else structuur eruit halen delegeren naar een controller per onderwerp (bijvoorbeeld vehicle) maakt code herbruikbaarder en uitbreidbaarder

FRONT CONTROLLER OO VIA $_GET Voorbeelden http://example.com/index.php? controller=vehicle&action=new http://example.com/index.php? controller=vehicle&action=detail&id=1 Zie dynweb2013examples/theorie07/ frontcontroller_stap02

VOORBEELD VEHICLE OVERVIEW http://example.com/index.php? controller=vehicle&action=index $object = VehicleController ; $method = index ; $object = new $object(); $object->$method();

INDEX.PHP <?php require_once('controller.php'); $controller = new Controller(); $controller->run(); Code Onveranderd

CONTROLLER.PHP <?php class Controller{ public function run() { define('default_controller', 'home'); define('default_action', 'index'); // getting the requested controller from $_GET['controller'] $controller = (isset($_get['controller']))? $_GET['controller'] : DEFAULT_CONTROLLER; // setting the object and including the controller objectfile $object = ucfirst(strtolower($controller)); $file = $object. 'Controller'. '.php'; require_once($file); // getting the requested action frm $_GET['action'] $method = (isset($_get['action']))? $_GET['action'] : DEFAULT_ACTION; $object = $object. 'Controller'; // creating the controller as an instance of the controller object $object = new $object(); // executing the method $object->$method(); $object = Vehicle ; $file = VehicleController.php $method = index ; $object = VehicleController $object = new $object(); $object->$method();

VEHICLECONTROLLER.PHP <?php class VehicleController { private $_vehiclemapper; public function construct() { $this->_vehiclemapper = new VehicleMapper(); public function index() { $vehicles = $this->_vehiclemapper->getall(); require_once('vehicle_overview.php'); public function detail() {... public function add() {... public function register() {...

<?php require_once('db.php'); require_once('vehicle.php'); class VehicleMapper { private $_db; VEHICLEMAPPER.PHP public function construct() { $this->_db = Db::getInstance(); public function add($object) {... public function getall() { $sql = "SELECT * FROM vehicles"; $data = $this->_db->query($sql); $objects = array(); foreach ($data as $row) { $object = new Vehicle($row['color'], $row['brand']); $objects[] = $object; return $objects; Code Onveranderd

VEHICLE.PHP <?php class Vehicle { private $color; private $brand;?> public function construct($color = 'blauw', $brand = 'audi') { $this->setcolor($color); $this->setbrand($brand); public function tostring() { return 'een voertuig met de kleur '. $this->getcolor(). ' en het merk '. $this->getbrand(); public function setcolor($color){ $this->color = $color; public function getcolor(){ return $this->color; public function setbrand($brand){ $this->brand = $brand; public function getbrand(){ return $this->brand; Code Onveranderd

VEHICLE_OVERVIEW.PHP <!DOCTYPE html> <html> <body> <a href="index.php?controller=home&action=home">home</a> <a href="index.php?controller=vehicle&action=add">nieuwe registratie</a> <br/> <br/> <table> <th>kleur</th> <th>merk</th> <?php foreach ($vehicles as $vehicle) {?> <tr> <td><?php echo $vehicle->getcolor();?></td> <td><?php echo $vehicle->getbrand();?></td> </tr> <?php?> </table> </body> </html>

FRONT CONTROLLER OO VIA $_GET Uitbreiding als we een detail pagina willen toevoegen waarmee we alle informatie van dat specifiek vehicle willen tonen

FRONT CONTROLLER OO VIA $_GET Problemen? URLs worden te lang en minder leesbaar URLs niet conform de standaard

FRONT CONTROLLER ZONDER $_GET Leesbaarheid verhogen van URLs http://example.com/controller/action/parameters

FRONT CONTROLLER ZONDER $_GET Voorbeelden http://example.com/vehicle/new http://example.com/vehicle/detail/1 Zie dynweb2013examples/theorie07/ frontcontroller_stap03

FRONT CONTROLLER ZONDER $_GET 2 stappen Stap 1: de webserver zeggen dat alles via index.php moet gaan altijd Stap 2: alle informatie uit de URL halen en mappen op de juiste controller, action en parameters

STAP 1 via mod-rewrite van apache (webserver).htaccess file maken met daar code in http://www.addedbytes.com/articles/forbeginners/url-rewriting-for-beginners/

.HTACCESS RewriteEngine On // activate the rewrite engine RewriteCond %{REQUEST_FILENAME -s [OR] // if (requested filename is a file with a size > 0 RewriteCond %{REQUEST_FILENAME -l [OR] // or requested filename is a symbolic link RewriteCond %{REQUEST_FILENAME -d // or requested filename is a directory) RewriteRule ^.*$ - [NC,L] // then show the requested filename RewriteRule ^.*$ index.php [NC,L] // else redirect to index.php page

STAP 2 Mappen structuur volgens MVC patroon application controller model view system controller model view

STAP 2 Controller klasse wordt gerefactord

VOORBEELD VEHICLE DETAIL http://example.com/vehicle/detail/1

INDEX.PHP <?php session_start(); // constanten ivm de paths define('application_path', 'application/'); define('system_path', 'system/'); require_once(application_path. 'config.php'); require_once(system_path. 'model/db.php'); require_once(system_path. 'controller/controller.php'); $controller = new Controller(); $controller->run();

CONTROLLER.PHP <?php class Controller { const DEFAULT_CONTROLLER = "Home"; const DEFAULT_ACTION = "index"; const CONTROLLER_PATH = 'application/controller/'; const CONTROLLER_FILE = 'index.php'; private $controller = self::default_controller; private $action = self::default_action; private $params = array(); public function construct() { $this->parseuri();

CONTROLLER.PHP private function parseuri() { // strip the controllerfile out of the scriptname $scriptprefix = str_replace(self::controller_file, '', $_SERVER['SCRIPT_NAME']); $uri = str_replace(self::controller_file, '', $_SERVER['REQUEST_URI']); // get the part of the uri, starting from the position after the scriptprefix $path = substr($uri, strlen($scriptprefix)); // strip non-alphanumeric characters out of the path $path = preg_replace('/[^a-za-z0-9]\//', "", $path); // trim the path for / $path = trim($path, '/'); $_SERVER[ SCRIPT_NAME ] = /2013/lessons/theorie07/frontcontroller_stap03/index.php $_SERVER[ REQUEST_URI ] = /2013/lessons/theorie07/frontcontroller_stap03/vehicle/detail/1 $scriptprefix = /2013/lessons/theorie07/frontcontroller_stap03/ $uri = /2013/lessons/theorie07/frontcontroller_stap03/vehicle/detail/1 $path = vehicle/detail/1

CONTROLLER.PHP // explode the $path into three parts to get the controller, action and parameters // the @-sign is used to supress errors when the function after it fails @list($controller, $action, $params) = explode("/", $path, 3); if (isset($controller)) { $this->setcontroller($controller); if (isset($action)) { $this->setaction($action); if (isset($params)) { $this->setparams(explode("/", $params)); $controller = vehicle $action = detail $params = 1

CONTROLLER.PHP private function setcontroller($controller) { $controller = ($controller)? $controller : self::default_controller; $controllerfile = self::controller_path. ucfirst(strtolower($controller)). 'Controller'. '.php'; // check if controller file exists if (!file_exists($controllerfile)) { die("controller '$controller' could not be found."); else { require_once($controllerfile); $this->controller = $controller. 'Controller'; return $this; private function setaction($action) {...) private function setparams(array $params) {... $this->controller = vehiclecontroller $this->action = detail $this->params = Array array(1) { [0]=> string(1) "1"

CONTROLLER.PHP public function run() { // checking the parameter count, using Reflection (http://www.php.net/reflection) $reflector = new ReflectionClass($this->controller); $method = $reflector->getmethod($this->action); $parameters = $method->getnumberofrequiredparameters(); if (($parameters) > count($this->params)) { die("action '$this->action' in class '$this->controller' expects $parameters mandatory parameter(s), you only provided ". count($this->params). "."); // create an instance of the controller as an object $controller = new $this->controller(); // call the method based on $this->action and the params call_user_func_array(array($controller, $this->action), $this->params); $reflector = Class [ class VehicleController ] $method = Method [ public method detail ] $parameters = 1

VEHICLECONTROLLER.PHP <?php class VehicleController { private $_vehiclemapper; private $_vehicle; public function construct () { require_once(application_path. 'model/vehiclemapper.php'); $this->_vehiclemapper = new VehicleMapper(); public function index() {... public function detail($id) { $this->_vehicle = $this->_vehiclemapper->get($id); require_once(application_path.'view/vehicle_detail.php'); public function add() {... Code Onveranderd

<?php require_once(system_path. 'model/db.php'); require_once(application_path. 'model/vehicle.php'); class VehicleMapper { private $_db; VEHICLEMAPPER.PHP public function construct() { $this->_db = Db::getInstance(); public function add($object) {... public function getall() {... public function get($id) { $id = (int)$id; $query = " SELECT * FROM vehicles WHERE id =?; "; return $this->_db->queryone($query, 'Vehicle', array($id));

DB.PHP <?php class Db { private static $instance = null; private $_db;... public function execute($sql, $arguments = array()) { if (!is_array($arguments)) { $arguments = array($arguments); try { $stmt = $this->_db->prepare($sql); $stmt->execute($arguments); $stmt->setfetchmode(pdo::fetch_assoc); catch(pdoexception $e) { error_log($e->getmessage()); return $stmt;

DB.PHP public function queryall($sql, $type, $arguments = array()) { $stmt = $this->execute($sql, $arguments); return $stmt->fetchall(pdo::fetch_class, $type); public function queryone($sql, $type, $arguments = array()) { $stmt = $this->execute($sql, $arguments); return $stmt->fetchobject($type);

<?php class Vehicle { private $color; private $brand; VEHICLE.PHP public function construct() { public function tostring() { return 'een voertuig met de kleur '. $this->getcolor(). ' en het merk '. $this->getbrand(); public function setcolor($color) { $this->color = $color; public function getcolor() { return $this->color; public function setbrand($brand) { $this->brand = $brand; public function getbrand() { return $this->brand;

VEHICLE_DETAIL.PHP <!DOCTYPE html> <html> <body> <table> <th>kleur</th> <th>merk</th> <tr> <td><?php echo $this->_vehicle->getcolor();?></td> <td><?php echo $this->_vehicle->getbrand();?></td> </tr> </table> </body> </html> Code Onveranderd

FRONT CONTROLLER ZONDER $_GET Uitbreiding Categorie toevoegen

FRONT CONTROLLER ZONDER $_GET Problemen? veel dubbele code nu

FRONT CONTROLLER ZONDER $_GET Refactoren dubbele code Mapper klasse Identifiable klasse Zie dynweb2013examples/theorie07/ frontcontroller_stap04

MAPPER.PHP <?php require_once(system_path. 'model/db.php'); class Mapper {... protected $_db; protected $_table; protected $_type; public function construct($table, $type) { $this->_db = Db::getInstance(); $this->_table = $table; $this->_type = $type;

MAPPER.PHP public function get($id) { $query = " SELECT * FROM $this->_table WHERE id =? "; return $this->_db->queryone($query, $this->_type, array($id)); public function getall() { $query = " SELECT * FROM $this->_table "; return $this->_db->queryall($query, $this->_type);...

VEHICLEMAPPER.PHP <?php require_once(application_path. 'model/vehicle.php'); require_once(system_path. 'model/mapper.php'); class VehicleMapper extends Mapper { public function construct() { parent:: construct('vehicles', 'Vehicle');

IDENTIFIABLE.PHP <?php class Identifiable { private $_id; public function getid() { return $this->_id; public function setid($id) { $this->_id = $id; function get($property) { $method = "get{$property"; if (method_exists($this, $method)) { return $this->$method(); public function set($property, $value){ $method = "set{$property"; if (method_exists($this, $method)) { $this->$method($value);

<?php require_once(system_path. 'model/identifiable.php'); class Vehicle extends Identifiable { private $_color; private $_brand; public function construct() { public function setcolor($color) { $this->_color = $color; public function getcolor() { return $this->_color; public function setbrand($brand) { $this->_brand = $brand; public function getbrand() { return $this->_brand; VEHICLE.PHP

VEHICLE.PHP public function tostring() { return 'een voertuig met de kleur '. $this->getcolor(). ' en het merk '. $this->getbrand(); public function toarray() { $fields['color'] = $this->_color; $fields['brand'] = $this->_brand; return $fields;

AGENDA Lesschema Herhaling Front Controller?

VOORBEREIDING LABO Bekijk de code in svn dynweb2013examples/theorie07/frontcontroller

VOORBEREIDING LABO URLs home home/display/elke vehicle vehicle/detail/1 category category/detail/2