SlideShare a Scribd company logo
1 of 22
PHPBelgium event 2009-06-24




              PHP & the MVC Pattern


http://joind.in/609                       Patrick Allaert


        PHPBelgium – Belgian PHP community
                         http://www.phpbelgium.be/
About Me
Started developping using PHP 3
Ten years of FOSS evangelism
Contribute to different FOSS:
    Tabellio (Open Source Collaboration for assemblies)
    Xoo°f (eXtensible Open Object Oriented Framework)
    KDESvn (Subversion frontend for KDE)
    PHP (ext/ldap)
APM lead developer (Alternative PHP Monitor)
Working @ AUSY
PHPBelgium staff member
Blog: http://patrickallaert.blogspot.com/
Twitter: http://twitter.com/patrick_allaert
               PHPBelgium – Belgian PHP community
                                              http://www.phpbelgium.be/
Model-View-Controller (1)
“… is an architectural pattern used in software
 engineering. Successful use of the pattern
 isolates business logic from user interface
 considerations, resulting in an application
 where it is easier to modify either the visual
 appearance of the application or the underlying
 business rules without affecting the other.”
                                                 Wikipedia


        PHPBelgium – Belgian PHP community
                         http://www.phpbelgium.be/
Model-View-Controller (2)


                                           Model
                                                          DB


Requests       Controller


                                                         (X)HTML
                                           View            XML




            PHPBelgium – Belgian PHP community
                             http://www.phpbelgium.be/
Design of some PHP applications




    PHPBelgium – Belgian PHP community
                     http://www.phpbelgium.be/
Managing this is sometimes...




           very difficult...
   PHPBelgium – Belgian PHP community
                    http://www.phpbelgium.be/
Demo application




/index.php                  /team.php?id=<TeamID>

 PHPBelgium – Belgian PHP community
                  http://www.phpbelgium.be/
Spaghetti code
<?php                                                       <table id="ranking">
$db = new mysqli("localhost", "foot", "footpw", "foot");      <tr>

$result = $db->query(
                                                                <th>Name</th>Presentation
                                                                <th>Points</th>
  "SELECT name, won, lost, draw, coach,                       </tr>
                Business Logic
     won * 3 + draw AS points                               <?php
   FROM team
   WHERE id = " . (int) $_GET['id']);                                     Business Logic
                                                            $result = $db->query("SELECT * FROM ranking");
                                                            while ($row = $result->fetch_assoc()) {
$team = $result->fetch_assoc();                             ?>
?>                                                            <tr>
<html>                                                          <td class="name">
<head>                                                            <a href="team.php?id=<?= $row['id'] ?>">
  <title>Team - <?= $team['name'] ?></title>                        <?= $row['name'] ?>
</head>                                                           </a>
<body id="team">                                                </td>
<h1>Team: <?= $team['name'] ?></h1>                             <td class="points">
<table id="info">                                                 <?= $row['points'] ?>
                  Presentation
  <tr><th>Won:</th>   <td><?= $team['won'] ?></td></tr>         </td>        Presentation
  <tr><th>Lost:</th>  <td><?= $team['lost'] ?></td></tr>      </tr>
  <tr><th>Draw:</th>  <td><?= $team['draw'] ?></td></tr>    <?php
  <tr><th>Points:</th><td><?= $team['points'] ?             }
></td></tr>                                                 ?>
  <tr><th>Coach:</th> <td><?= $team['coach'] ?></td></tr>   </table>
</table>                                                    <a href="/">Ranking</a>
                                                            </body>
                                                            </html>




                       PHPBelgium – Belgian PHP community
                                                            http://www.phpbelgium.be/
Taking the Business Logic apart (1)




      PHPBelgium – Belgian PHP community
                       http://www.phpbelgium.be/
Taking the Business Logic apart (2)
             class Team {
               public static function getInformationById($id) {
                 return Database::getInstance()
                   ->query(
                   "SELECT name, won, lost, draw, coach, won*3+draw AS points " .
                   "FROM team WHERE id = " . (int) $id)
                   ->fetch_assoc();
               }
             }

             class Ranking {
               public static function generate() {
                 return Database::getInstance()
                   ->query("SELECT * FROM ranking")
                   ->fetch_all();
               }
             }

             class Database {
               public static function getInstance() {
                 static $connection = null;

                 if ($connection === null) {
                   $connection = new mysqli("localhost", "foot", "footpw", "foot");
                 }

                 return $connection;
               }
             }



      PHPBelgium – Belgian PHP community
                               http://www.phpbelgium.be/
Taking the Business Logic apart (3)
 $position = 1;
 foreach (Ranking::generate() as $team) {
 ?>
     <tr>
         <td class="position"><?= $position++ ?></td>
         <td class="name"><a href="team.php?id=<?= $team['id'] ?>"><?= $team['name'] ?></a></td>
         <td class="played"><?= $team['played'] ?></td>
         <td class="won"><?= $team['won'] ?></td>
         <td class="lost"><?= $team['lost'] ?></td>
         <td class="draw"><?= $team['draw'] ?></td>
         <td class="points"><?= $team['points'] ?></td>
     </tr>
 <?php
 }

 <?php
 require 'Team.php';
 require 'Ranking.php';
 $team = Team::getInformationById($_GET['id']);
 ?>
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

 foreach (Ranking::generate() as $row) {
 ?>
     <tr<?php if ($row['id'] === $_GET['id']) { ?> class="selected"<?php } ?>>
         <td class="name"><a href="team.php?id=<?= $row['id'] ?>"><?= $row['name'] ?></a></td>
         <td class="points"><?= $row['points'] ?></td>
     </tr>
 <?php
 }




              PHPBelgium – Belgian PHP community
                                                  http://www.phpbelgium.be/
Evaluating the situation
Model is reusable         Views tightly linked to
Changes to the              the model
 business logic does      Views impacted if the
 not require inspecting     API of the model
 all files                  change
                          Calls to the model may
                           be difficult to find:
                           spread everywhere in
                           views

        PHPBelgium – Belgian PHP community
                          http://www.phpbelgium.be/
On the path to MVC (1)
                                             <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
                                             <html>
                                             <head>
                                               <title>Team - <?= $GLOBALS['team']['name'] ?></title>
                                             </head>
                                             <body id="team">
                                             <h1>Team: <?= $GLOBALS['team']['name'] ?></h1>
                                             <table id="info">
                                               <tr><th>Won:</th>   <td><?= $GLOBALS['team']['won'] ?></td></tr>
                                               <tr><th>Lost:</th>  <td><?= $GLOBALS['team']['lost'] ?></td></tr>
                                               <tr><th>Draw:</th>  <td><?= $GLOBALS['team']['draw'] ?></td></tr>
                                               <tr><th>Points:</th><td><?= $GLOBALS['team']['points'] ?></td></tr>
                                               <tr><th>Coach:</th> <td><?= $GLOBALS['team']['coach'] ?></td></tr>
                                             </table>
                                             <table id="ranking">
                                               <tr>
         <?php
                                                 <th>Name</th><th>Points</th>
         require 'Ranking.php';
                                               </tr>
                                             <?php
         // View variables
                                             foreach ($GLOBALS['ranking'] as $row) {
         $ranking = Ranking::generate();
                                             ?>
                                               <tr>
         require 'home.tpl';
                                                 <td class="name">
                                                   <a href="team.php?id=<?= $row['id'] ?>"><?= $row['name'] ?></a>
<?php                                            </td>
require 'Team.php';                              <td class="points"><?= $row['points'] ?></td>
require 'Ranking.php';                         </tr>
                                             <?php
// View variables                            }
$teamId = $_GET['id'];                       ?>
$team = Team::getInformationById($teamId);   </table>
$ranking = Ranking::generate();              </body>
                                             </html>
require 'team.tpl';




                         PHPBelgium – Belgian PHP community
                                                        http://www.phpbelgium.be/
On the path to MVC (2)
View parameters are passed using global
  variables
  Hint: $variableName may also be written:
  $GLOBALS['variableName']
  The latest form may be used to differentiate
  variables defined by the controller file from the
  local view variables
“htdocs” could be renamed “controllers”


         PHPBelgium – Belgian PHP community
                           http://www.phpbelgium.be/
Howto: Clean URL
Clean URL: Transforming
   http://example.com/index.php?type=article&id=25&date=20020322
   into:
   http://example.com/article/200203226
Enables better search engine indexing
URIs are not supposed to change (Cool URIs don't change)
Doesn't expose the server-side language
Most MVC Frameworks provide clean URL mechanism, how to setup one with
  our structure?




             PHPBelgium – Belgian PHP community
                                        http://www.phpbelgium.be/
Clean URL: method #1
Using Apache's mod_rewrite:
 RewriteEngine On
 RewriteRule ^/team/([0-9]*)$ /team.php?id=$1 [L]

Ability to transform URLs on the fly and mapping
 part of the URL directly to a $_GET variable




        PHPBelgium – Belgian PHP community
                         http://www.phpbelgium.be/
Clean URL: method #2
Dropping the “.php” extension and forcing the
 mime-type/handler used in the Apache
 configuration

<FilesMatch "^(index|team)$">
      ForceType application/x-httpd-php
  </FilesMatch>
 or:
 <FilesMatch "^(index|team)$">
     SetHandler application/x-httpd-php
 </FilesMatch>


        PHPBelgium – Belgian PHP community
                         http://www.phpbelgium.be/
Performance impact (index page)

 Concurrency           1                           15
 “spaghetti”         1776.65                     3545.77
 Model-View          1615.43                     3137.52
 MVC                 1602.57                     3091.74
 Clean URL #1        1554.86                     3032.25
 Clean URL #2        1686.85                     3294.60
 Zend Framework      246.36                      421.15
                               In requests/second, higher = better




         PHPBelgium – Belgian PHP community
                               http://www.phpbelgium.be/
Performance impact (team page)

 Concurrency           1                           15
 “spaghetti”         1778.89                     3507.61
 Model-View          1580.92                     3065.41
 MVC                 1546.32                     3032.64
 Clean URL #1        1519.59                     2986.12
 Clean URL #2        1465.43                     2896.54
 Zend Framework      233.94                      402.54
                               In requests/second, higher = better




         PHPBelgium – Belgian PHP community
                               http://www.phpbelgium.be/
Questions ?




PHPBelgium – Belgian PHP community
                 http://www.phpbelgium.be/
Thanks :-)



These slides will be available through:
  http://patrickallaert.blogspot.com/

     You can vote for this talk on:
          http://joind.in/609



   PHPBelgium – Belgian PHP community
                    http://www.phpbelgium.be/
License
This presentation material is published under the Creative Commons Attribution-Share Alike 3.0 Unported license.

You are free:

to Share — to copy, distribute and transmit the work

to Remix — to adapt the work

Under the following conditions:

Attribution — You must attribute the work in the manner specified by the author or licensor (but not in any way that
  suggests that they endorse you or your use of the work).

Share Alike — If you alter, transform, or build upon this work, you may distribute the resulting work only under the
  same, similar or a compatible license.

With the understanding that:

Waiver — Any of the above conditions can be waived if you get permission from the copyright holder.

Other Rights — In no way are any of the following rights affected by the license:

  Your fair dealing or fair use rights;
  The author's moral rights;
  Rights other persons may have either in the work itself or in how the work is used, such as publicity or privacy
    rights.


                         PHPBelgium – Belgian PHP community
                                                             http://www.phpbelgium.be/

More Related Content

Viewers also liked

PHP Frameworks and CodeIgniter
PHP Frameworks and CodeIgniterPHP Frameworks and CodeIgniter
PHP Frameworks and CodeIgniterKHALID C
 
Intro To Mvc Development In Php
Intro To Mvc Development In PhpIntro To Mvc Development In Php
Intro To Mvc Development In Phpfunkatron
 
Principles of MVC for PHP Developers
Principles of MVC for PHP DevelopersPrinciples of MVC for PHP Developers
Principles of MVC for PHP DevelopersEdureka!
 
Why to choose laravel framework
Why to choose laravel frameworkWhy to choose laravel framework
Why to choose laravel frameworkBo-Yi Wu
 
How to choose web framework
How to choose web frameworkHow to choose web framework
How to choose web frameworkBo-Yi Wu
 
REST API Best Practices & Implementing in Codeigniter
REST API Best Practices & Implementing in CodeigniterREST API Best Practices & Implementing in Codeigniter
REST API Best Practices & Implementing in CodeigniterSachin G Kulkarni
 
CodeIgniter PHP MVC Framework
CodeIgniter PHP MVC FrameworkCodeIgniter PHP MVC Framework
CodeIgniter PHP MVC FrameworkBo-Yi Wu
 
PHP & JavaScript & CSS Coding style
PHP & JavaScript & CSS Coding stylePHP & JavaScript & CSS Coding style
PHP & JavaScript & CSS Coding styleBo-Yi Wu
 
RESTful API Design & Implementation with CodeIgniter PHP Framework
RESTful API Design & Implementation with CodeIgniter PHP FrameworkRESTful API Design & Implementation with CodeIgniter PHP Framework
RESTful API Design & Implementation with CodeIgniter PHP FrameworkBo-Yi Wu
 
Write microservice in golang
Write microservice in golangWrite microservice in golang
Write microservice in golangBo-Yi Wu
 
Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingAhmed Swilam
 
Class 5 - PHP Strings
Class 5 - PHP StringsClass 5 - PHP Strings
Class 5 - PHP StringsAhmed Swilam
 
Class 4 - PHP Arrays
Class 4 - PHP ArraysClass 4 - PHP Arrays
Class 4 - PHP ArraysAhmed Swilam
 
Class 8 - Database Programming
Class 8 - Database ProgrammingClass 8 - Database Programming
Class 8 - Database ProgrammingAhmed Swilam
 
Class 3 - PHP Functions
Class 3 - PHP FunctionsClass 3 - PHP Functions
Class 3 - PHP FunctionsAhmed Swilam
 
Class 1 - World Wide Web Introduction
Class 1 - World Wide Web IntroductionClass 1 - World Wide Web Introduction
Class 1 - World Wide Web IntroductionAhmed Swilam
 
Class 2 - Introduction to PHP
Class 2 - Introduction to PHPClass 2 - Introduction to PHP
Class 2 - Introduction to PHPAhmed Swilam
 
Class 6 - PHP Web Programming
Class 6 - PHP Web ProgrammingClass 6 - PHP Web Programming
Class 6 - PHP Web ProgrammingAhmed Swilam
 

Viewers also liked (19)

PHP Frameworks and CodeIgniter
PHP Frameworks and CodeIgniterPHP Frameworks and CodeIgniter
PHP Frameworks and CodeIgniter
 
Intro To Mvc Development In Php
Intro To Mvc Development In PhpIntro To Mvc Development In Php
Intro To Mvc Development In Php
 
Principles of MVC for PHP Developers
Principles of MVC for PHP DevelopersPrinciples of MVC for PHP Developers
Principles of MVC for PHP Developers
 
Why to choose laravel framework
Why to choose laravel frameworkWhy to choose laravel framework
Why to choose laravel framework
 
How to choose web framework
How to choose web frameworkHow to choose web framework
How to choose web framework
 
REST API Best Practices & Implementing in Codeigniter
REST API Best Practices & Implementing in CodeigniterREST API Best Practices & Implementing in Codeigniter
REST API Best Practices & Implementing in Codeigniter
 
CodeIgniter PHP MVC Framework
CodeIgniter PHP MVC FrameworkCodeIgniter PHP MVC Framework
CodeIgniter PHP MVC Framework
 
PHP & JavaScript & CSS Coding style
PHP & JavaScript & CSS Coding stylePHP & JavaScript & CSS Coding style
PHP & JavaScript & CSS Coding style
 
PHP MVC
PHP MVCPHP MVC
PHP MVC
 
RESTful API Design & Implementation with CodeIgniter PHP Framework
RESTful API Design & Implementation with CodeIgniter PHP FrameworkRESTful API Design & Implementation with CodeIgniter PHP Framework
RESTful API Design & Implementation with CodeIgniter PHP Framework
 
Write microservice in golang
Write microservice in golangWrite microservice in golang
Write microservice in golang
 
Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented Programming
 
Class 5 - PHP Strings
Class 5 - PHP StringsClass 5 - PHP Strings
Class 5 - PHP Strings
 
Class 4 - PHP Arrays
Class 4 - PHP ArraysClass 4 - PHP Arrays
Class 4 - PHP Arrays
 
Class 8 - Database Programming
Class 8 - Database ProgrammingClass 8 - Database Programming
Class 8 - Database Programming
 
Class 3 - PHP Functions
Class 3 - PHP FunctionsClass 3 - PHP Functions
Class 3 - PHP Functions
 
Class 1 - World Wide Web Introduction
Class 1 - World Wide Web IntroductionClass 1 - World Wide Web Introduction
Class 1 - World Wide Web Introduction
 
Class 2 - Introduction to PHP
Class 2 - Introduction to PHPClass 2 - Introduction to PHP
Class 2 - Introduction to PHP
 
Class 6 - PHP Web Programming
Class 6 - PHP Web ProgrammingClass 6 - PHP Web Programming
Class 6 - PHP Web Programming
 

More from Patrick Allaert

Advanced debugging techniques (PHP)
Advanced debugging techniques (PHP)Advanced debugging techniques (PHP)
Advanced debugging techniques (PHP)Patrick Allaert
 
PHP data structures (and the impact of php 7 on them), phpDay Verona 2015, Italy
PHP data structures (and the impact of php 7 on them), phpDay Verona 2015, ItalyPHP data structures (and the impact of php 7 on them), phpDay Verona 2015, Italy
PHP data structures (and the impact of php 7 on them), phpDay Verona 2015, ItalyPatrick Allaert
 
La métrique, ce n'est pas que pour le devops
La métrique, ce n'est pas que pour le devopsLa métrique, ce n'est pas que pour le devops
La métrique, ce n'est pas que pour le devopsPatrick Allaert
 
Maitriser les structures de données PHP 102 - Forum Paris 2012
Maitriser les structures de données PHP 102 - Forum Paris 2012Maitriser les structures de données PHP 102 - Forum Paris 2012
Maitriser les structures de données PHP 102 - Forum Paris 2012Patrick Allaert
 
Create your own PHP extension, step by step - phpDay 2012 Verona
Create your own PHP extension, step by step - phpDay 2012 VeronaCreate your own PHP extension, step by step - phpDay 2012 Verona
Create your own PHP extension, step by step - phpDay 2012 VeronaPatrick Allaert
 
Mastering PHP Data Structure 102 - phpDay 2012 Verona
Mastering PHP Data Structure 102 - phpDay 2012 VeronaMastering PHP Data Structure 102 - phpDay 2012 Verona
Mastering PHP Data Structure 102 - phpDay 2012 VeronaPatrick Allaert
 
Masterizing PHP Data Structure 102 - PHPUK 2012
Masterizing PHP Data Structure 102 - PHPUK 2012Masterizing PHP Data Structure 102 - PHPUK 2012
Masterizing PHP Data Structure 102 - PHPUK 2012Patrick Allaert
 
Masterizing php data structure 102
Masterizing php data structure 102Masterizing php data structure 102
Masterizing php data structure 102Patrick Allaert
 
PHP applications/environments monitoring: APM & Pinba
PHP applications/environments monitoring: APM & PinbaPHP applications/environments monitoring: APM & Pinba
PHP applications/environments monitoring: APM & PinbaPatrick Allaert
 
Monitoring d'applications/environnements PHP: APM et Pinba
Monitoring d'applications/environnements PHP: APM et PinbaMonitoring d'applications/environnements PHP: APM et Pinba
Monitoring d'applications/environnements PHP: APM et PinbaPatrick Allaert
 
MVC = Make Venerated Code?
MVC = Make Venerated Code?MVC = Make Venerated Code?
MVC = Make Venerated Code?Patrick Allaert
 

More from Patrick Allaert (11)

Advanced debugging techniques (PHP)
Advanced debugging techniques (PHP)Advanced debugging techniques (PHP)
Advanced debugging techniques (PHP)
 
PHP data structures (and the impact of php 7 on them), phpDay Verona 2015, Italy
PHP data structures (and the impact of php 7 on them), phpDay Verona 2015, ItalyPHP data structures (and the impact of php 7 on them), phpDay Verona 2015, Italy
PHP data structures (and the impact of php 7 on them), phpDay Verona 2015, Italy
 
La métrique, ce n'est pas que pour le devops
La métrique, ce n'est pas que pour le devopsLa métrique, ce n'est pas que pour le devops
La métrique, ce n'est pas que pour le devops
 
Maitriser les structures de données PHP 102 - Forum Paris 2012
Maitriser les structures de données PHP 102 - Forum Paris 2012Maitriser les structures de données PHP 102 - Forum Paris 2012
Maitriser les structures de données PHP 102 - Forum Paris 2012
 
Create your own PHP extension, step by step - phpDay 2012 Verona
Create your own PHP extension, step by step - phpDay 2012 VeronaCreate your own PHP extension, step by step - phpDay 2012 Verona
Create your own PHP extension, step by step - phpDay 2012 Verona
 
Mastering PHP Data Structure 102 - phpDay 2012 Verona
Mastering PHP Data Structure 102 - phpDay 2012 VeronaMastering PHP Data Structure 102 - phpDay 2012 Verona
Mastering PHP Data Structure 102 - phpDay 2012 Verona
 
Masterizing PHP Data Structure 102 - PHPUK 2012
Masterizing PHP Data Structure 102 - PHPUK 2012Masterizing PHP Data Structure 102 - PHPUK 2012
Masterizing PHP Data Structure 102 - PHPUK 2012
 
Masterizing php data structure 102
Masterizing php data structure 102Masterizing php data structure 102
Masterizing php data structure 102
 
PHP applications/environments monitoring: APM & Pinba
PHP applications/environments monitoring: APM & PinbaPHP applications/environments monitoring: APM & Pinba
PHP applications/environments monitoring: APM & Pinba
 
Monitoring d'applications/environnements PHP: APM et Pinba
Monitoring d'applications/environnements PHP: APM et PinbaMonitoring d'applications/environnements PHP: APM et Pinba
Monitoring d'applications/environnements PHP: APM et Pinba
 
MVC = Make Venerated Code?
MVC = Make Venerated Code?MVC = Make Venerated Code?
MVC = Make Venerated Code?
 

Recently uploaded

AI mind or machine power point presentation
AI mind or machine power point presentationAI mind or machine power point presentation
AI mind or machine power point presentationyogeshlabana357357
 
Working together SRE & Platform Engineering
Working together SRE & Platform EngineeringWorking together SRE & Platform Engineering
Working together SRE & Platform EngineeringMarcus Vechiato
 
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptxHarnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptxFIDO Alliance
 
State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!Memoori
 
The Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightThe Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightSafe Software
 
Top 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development CompaniesTop 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development CompaniesTopCSSGallery
 
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdfThe Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdfFIDO Alliance
 
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...ScyllaDB
 
Event-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream ProcessingEvent-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream ProcessingScyllaDB
 
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdfHow Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdfFIDO Alliance
 
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdfLinux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdfFIDO Alliance
 
Introduction to FIDO Authentication and Passkeys.pptx
Introduction to FIDO Authentication and Passkeys.pptxIntroduction to FIDO Authentication and Passkeys.pptx
Introduction to FIDO Authentication and Passkeys.pptxFIDO Alliance
 
Where to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdfWhere to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdfFIDO Alliance
 
Using IESVE for Room Loads Analysis - UK & Ireland
Using IESVE for Room Loads Analysis - UK & IrelandUsing IESVE for Room Loads Analysis - UK & Ireland
Using IESVE for Room Loads Analysis - UK & IrelandIES VE
 
WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024Lorenzo Miniero
 
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc
 
TopCryptoSupers 12thReport OrionX May2024
TopCryptoSupers 12thReport OrionX May2024TopCryptoSupers 12thReport OrionX May2024
TopCryptoSupers 12thReport OrionX May2024Stephen Perrenod
 
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdfIntroduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdfFIDO Alliance
 
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...FIDO Alliance
 

Recently uploaded (20)

AI mind or machine power point presentation
AI mind or machine power point presentationAI mind or machine power point presentation
AI mind or machine power point presentation
 
Working together SRE & Platform Engineering
Working together SRE & Platform EngineeringWorking together SRE & Platform Engineering
Working together SRE & Platform Engineering
 
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptxHarnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
 
State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!
 
The Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightThe Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and Insight
 
Top 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development CompaniesTop 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development Companies
 
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdfThe Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
 
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
 
Event-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream ProcessingEvent-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream Processing
 
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdfHow Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
 
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdfLinux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
 
Introduction to FIDO Authentication and Passkeys.pptx
Introduction to FIDO Authentication and Passkeys.pptxIntroduction to FIDO Authentication and Passkeys.pptx
Introduction to FIDO Authentication and Passkeys.pptx
 
Where to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdfWhere to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdf
 
Using IESVE for Room Loads Analysis - UK & Ireland
Using IESVE for Room Loads Analysis - UK & IrelandUsing IESVE for Room Loads Analysis - UK & Ireland
Using IESVE for Room Loads Analysis - UK & Ireland
 
WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024
 
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
 
Overview of Hyperledger Foundation
Overview of Hyperledger FoundationOverview of Hyperledger Foundation
Overview of Hyperledger Foundation
 
TopCryptoSupers 12thReport OrionX May2024
TopCryptoSupers 12thReport OrionX May2024TopCryptoSupers 12thReport OrionX May2024
TopCryptoSupers 12thReport OrionX May2024
 
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdfIntroduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
 
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
 

PHP & the MVC Pattern

  • 1. PHPBelgium event 2009-06-24 PHP & the MVC Pattern http://joind.in/609 Patrick Allaert PHPBelgium – Belgian PHP community http://www.phpbelgium.be/
  • 2. About Me Started developping using PHP 3 Ten years of FOSS evangelism Contribute to different FOSS: Tabellio (Open Source Collaboration for assemblies) Xoo°f (eXtensible Open Object Oriented Framework) KDESvn (Subversion frontend for KDE) PHP (ext/ldap) APM lead developer (Alternative PHP Monitor) Working @ AUSY PHPBelgium staff member Blog: http://patrickallaert.blogspot.com/ Twitter: http://twitter.com/patrick_allaert PHPBelgium – Belgian PHP community http://www.phpbelgium.be/
  • 3. Model-View-Controller (1) “… is an architectural pattern used in software engineering. Successful use of the pattern isolates business logic from user interface considerations, resulting in an application where it is easier to modify either the visual appearance of the application or the underlying business rules without affecting the other.” Wikipedia PHPBelgium – Belgian PHP community http://www.phpbelgium.be/
  • 4. Model-View-Controller (2) Model DB Requests Controller (X)HTML View XML PHPBelgium – Belgian PHP community http://www.phpbelgium.be/
  • 5. Design of some PHP applications PHPBelgium – Belgian PHP community http://www.phpbelgium.be/
  • 6. Managing this is sometimes... very difficult... PHPBelgium – Belgian PHP community http://www.phpbelgium.be/
  • 7. Demo application /index.php /team.php?id=<TeamID> PHPBelgium – Belgian PHP community http://www.phpbelgium.be/
  • 8. Spaghetti code <?php <table id="ranking"> $db = new mysqli("localhost", "foot", "footpw", "foot");   <tr> $result = $db->query(     <th>Name</th>Presentation     <th>Points</th> "SELECT name, won, lost, draw, coach,   </tr> Business Logic won * 3 + draw AS points <?php FROM team WHERE id = " . (int) $_GET['id']); Business Logic $result = $db->query("SELECT * FROM ranking"); while ($row = $result->fetch_assoc()) { $team = $result->fetch_assoc(); ?> ?>   <tr> <html>     <td class="name"> <head>       <a href="team.php?id=<?= $row['id'] ?>">   <title>Team - <?= $team['name'] ?></title>         <?= $row['name'] ?> </head>       </a> <body id="team">     </td> <h1>Team: <?= $team['name'] ?></h1>     <td class="points"> <table id="info"> <?= $row['points'] ?> Presentation   <tr><th>Won:</th>   <td><?= $team['won'] ?></td></tr> </td> Presentation   <tr><th>Lost:</th>  <td><?= $team['lost'] ?></td></tr>   </tr>   <tr><th>Draw:</th>  <td><?= $team['draw'] ?></td></tr> <?php   <tr><th>Points:</th><td><?= $team['points'] ? } ></td></tr> ?>   <tr><th>Coach:</th> <td><?= $team['coach'] ?></td></tr> </table> </table> <a href="/">Ranking</a> </body> </html> PHPBelgium – Belgian PHP community http://www.phpbelgium.be/
  • 9. Taking the Business Logic apart (1) PHPBelgium – Belgian PHP community http://www.phpbelgium.be/
  • 10. Taking the Business Logic apart (2) class Team {   public static function getInformationById($id) {     return Database::getInstance() ->query(       "SELECT name, won, lost, draw, coach, won*3+draw AS points " .       "FROM team WHERE id = " . (int) $id) ->fetch_assoc();   } } class Ranking {   public static function generate() {     return Database::getInstance() ->query("SELECT * FROM ranking") ->fetch_all();   } } class Database {   public static function getInstance() {     static $connection = null;     if ($connection === null) {       $connection = new mysqli("localhost", "foot", "footpw", "foot");     }     return $connection;   } } PHPBelgium – Belgian PHP community http://www.phpbelgium.be/
  • 11. Taking the Business Logic apart (3) $position = 1; foreach (Ranking::generate() as $team) { ?>     <tr>         <td class="position"><?= $position++ ?></td>         <td class="name"><a href="team.php?id=<?= $team['id'] ?>"><?= $team['name'] ?></a></td>         <td class="played"><?= $team['played'] ?></td>         <td class="won"><?= $team['won'] ?></td>         <td class="lost"><?= $team['lost'] ?></td>         <td class="draw"><?= $team['draw'] ?></td>         <td class="points"><?= $team['points'] ?></td>     </tr> <?php } <?php require 'Team.php'; require 'Ranking.php'; $team = Team::getInformationById($_GET['id']); ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> foreach (Ranking::generate() as $row) { ?>     <tr<?php if ($row['id'] === $_GET['id']) { ?> class="selected"<?php } ?>>         <td class="name"><a href="team.php?id=<?= $row['id'] ?>"><?= $row['name'] ?></a></td>         <td class="points"><?= $row['points'] ?></td>     </tr> <?php } PHPBelgium – Belgian PHP community http://www.phpbelgium.be/
  • 12. Evaluating the situation Model is reusable Views tightly linked to Changes to the the model business logic does Views impacted if the not require inspecting API of the model all files change Calls to the model may be difficult to find: spread everywhere in views PHPBelgium – Belgian PHP community http://www.phpbelgium.be/
  • 13. On the path to MVC (1) <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head>   <title>Team - <?= $GLOBALS['team']['name'] ?></title> </head> <body id="team"> <h1>Team: <?= $GLOBALS['team']['name'] ?></h1> <table id="info">   <tr><th>Won:</th>   <td><?= $GLOBALS['team']['won'] ?></td></tr>   <tr><th>Lost:</th>  <td><?= $GLOBALS['team']['lost'] ?></td></tr>   <tr><th>Draw:</th>  <td><?= $GLOBALS['team']['draw'] ?></td></tr>   <tr><th>Points:</th><td><?= $GLOBALS['team']['points'] ?></td></tr>   <tr><th>Coach:</th> <td><?= $GLOBALS['team']['coach'] ?></td></tr> </table> <table id="ranking">   <tr> <?php     <th>Name</th><th>Points</th> require 'Ranking.php';   </tr> <?php // View variables foreach ($GLOBALS['ranking'] as $row) { $ranking = Ranking::generate(); ?>   <tr> require 'home.tpl';     <td class="name"> <a href="team.php?id=<?= $row['id'] ?>"><?= $row['name'] ?></a> <?php </td> require 'Team.php';     <td class="points"><?= $row['points'] ?></td> require 'Ranking.php';   </tr> <?php // View variables } $teamId = $_GET['id']; ?> $team = Team::getInformationById($teamId); </table> $ranking = Ranking::generate(); </body> </html> require 'team.tpl'; PHPBelgium – Belgian PHP community http://www.phpbelgium.be/
  • 14. On the path to MVC (2) View parameters are passed using global variables Hint: $variableName may also be written: $GLOBALS['variableName'] The latest form may be used to differentiate variables defined by the controller file from the local view variables “htdocs” could be renamed “controllers” PHPBelgium – Belgian PHP community http://www.phpbelgium.be/
  • 15. Howto: Clean URL Clean URL: Transforming http://example.com/index.php?type=article&id=25&date=20020322 into: http://example.com/article/200203226 Enables better search engine indexing URIs are not supposed to change (Cool URIs don't change) Doesn't expose the server-side language Most MVC Frameworks provide clean URL mechanism, how to setup one with our structure? PHPBelgium – Belgian PHP community http://www.phpbelgium.be/
  • 16. Clean URL: method #1 Using Apache's mod_rewrite: RewriteEngine On RewriteRule ^/team/([0-9]*)$ /team.php?id=$1 [L] Ability to transform URLs on the fly and mapping part of the URL directly to a $_GET variable PHPBelgium – Belgian PHP community http://www.phpbelgium.be/
  • 17. Clean URL: method #2 Dropping the “.php” extension and forcing the mime-type/handler used in the Apache configuration <FilesMatch "^(index|team)$"> ForceType application/x-httpd-php </FilesMatch> or: <FilesMatch "^(index|team)$"> SetHandler application/x-httpd-php </FilesMatch> PHPBelgium – Belgian PHP community http://www.phpbelgium.be/
  • 18. Performance impact (index page) Concurrency 1 15 “spaghetti” 1776.65 3545.77 Model-View 1615.43 3137.52 MVC 1602.57 3091.74 Clean URL #1 1554.86 3032.25 Clean URL #2 1686.85 3294.60 Zend Framework 246.36 421.15 In requests/second, higher = better PHPBelgium – Belgian PHP community http://www.phpbelgium.be/
  • 19. Performance impact (team page) Concurrency 1 15 “spaghetti” 1778.89 3507.61 Model-View 1580.92 3065.41 MVC 1546.32 3032.64 Clean URL #1 1519.59 2986.12 Clean URL #2 1465.43 2896.54 Zend Framework 233.94 402.54 In requests/second, higher = better PHPBelgium – Belgian PHP community http://www.phpbelgium.be/
  • 20. Questions ? PHPBelgium – Belgian PHP community http://www.phpbelgium.be/
  • 21. Thanks :-) These slides will be available through: http://patrickallaert.blogspot.com/ You can vote for this talk on: http://joind.in/609 PHPBelgium – Belgian PHP community http://www.phpbelgium.be/
  • 22. License This presentation material is published under the Creative Commons Attribution-Share Alike 3.0 Unported license. You are free: to Share — to copy, distribute and transmit the work to Remix — to adapt the work Under the following conditions: Attribution — You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work). Share Alike — If you alter, transform, or build upon this work, you may distribute the resulting work only under the same, similar or a compatible license. With the understanding that: Waiver — Any of the above conditions can be waived if you get permission from the copyright holder. Other Rights — In no way are any of the following rights affected by the license: Your fair dealing or fair use rights; The author's moral rights; Rights other persons may have either in the work itself or in how the work is used, such as publicity or privacy rights. PHPBelgium – Belgian PHP community http://www.phpbelgium.be/