Presentation done on June the 24th on the MVC pattern at the PHPBelgium event in Mons.
How to refactor a simple spaghetti application into a well structured, MVC-aware (and framework-free) application with nice URL just using regular PHP and Apache's configuration.
Vote and comment it on: http://joind.in/609
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/
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/
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/
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/
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/