PHPBelgium event 18-02-2009




   MVC = Make Venerated Code?


                                     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)
Lead developer:
   APM (Alternative PHP Monitor)
Working @ AUSY
Blog: http://patrickallaert.blogspot.com/


             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




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)




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




      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)




PHPBelgium – Belgian PHP community
                 http://www.phpbelgium.be/
On the path to MVC (2)
Directory views to be added in the include_path
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=20020322into:
   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”         1483.15                     2798.23
 Model-View          1353.17                     2388.61
 MVC                 1339.72                     2364.66
 Clean URL #1        1320.61                     2303.99
 Clean URL #2        1412.43                     2468.83
 Zend Framework       164.11                     280.22
                               In requests/second, higher = better




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

 Concurrency           1                           15
 “spaghetti”         1372.66                     2428.79
 Model-View          1243.78                     2142.79
 MVC                 1235.48                     2364.66
 Clean URL #1        1232.39                     2090.51
 Clean URL #2        1192.78                     2040.44
 Zend Framework      158.77                      264.65
                               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/




   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.
Notice — For any reuse or distribution, you must make clear to others the license terms of this work.




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

MVC = Make Venerated Code?

  • 1.
    PHPBelgium event 18-02-2009 MVC = Make Venerated Code? Patrick Allaert PHPBelgium – Belgian PHP community http://www.phpbelgium.be/
  • 2.
    About Me Started developpingusing 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) Lead developer: APM (Alternative PHP Monitor) Working @ AUSY Blog: http://patrickallaert.blogspot.com/ PHPBelgium – Belgian PHP community http://www.phpbelgium.be/
  • 3.
    Model-View-Controller (1) “… isan 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 somePHP applications PHPBelgium – Belgian PHP community http://www.phpbelgium.be/
  • 6.
    Managing this issometimes... 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 PHPBelgium –Belgian PHP community http://www.phpbelgium.be/
  • 9.
    Taking the BusinessLogic apart (1) PHPBelgium – Belgian PHP community http://www.phpbelgium.be/
  • 10.
    Taking the BusinessLogic apart (2) PHPBelgium – Belgian PHP community http://www.phpbelgium.be/
  • 11.
    Taking the BusinessLogic apart (3) PHPBelgium – Belgian PHP community http://www.phpbelgium.be/
  • 12.
    Evaluating the situation Modelis 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 pathto MVC (1) PHPBelgium – Belgian PHP community http://www.phpbelgium.be/
  • 14.
    On the pathto MVC (2) Directory views to be added in the include_path 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 CleanURL: Transforming http://example.com/index.php?type=article&id=25&date=20020322into: 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 (indexpage) Concurrency 1 15 “spaghetti” 1483.15 2798.23 Model-View 1353.17 2388.61 MVC 1339.72 2364.66 Clean URL #1 1320.61 2303.99 Clean URL #2 1412.43 2468.83 Zend Framework 164.11 280.22 In requests/second, higher = better PHPBelgium – Belgian PHP community http://www.phpbelgium.be/
  • 19.
    Performance impact (teampage) Concurrency 1 15 “spaghetti” 1372.66 2428.79 Model-View 1243.78 2142.79 MVC 1235.48 2364.66 Clean URL #1 1232.39 2090.51 Clean URL #2 1192.78 2040.44 Zend Framework 158.77 264.65 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 slideswill be available through: http://patrickallaert.blogspot.com/ PHPBelgium – Belgian PHP community http://www.phpbelgium.be/
  • 22.
    License This presentation materialis 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. Notice — For any reuse or distribution, you must make clear to others the license terms of this work. PHPBelgium – Belgian PHP community http://www.phpbelgium.be/