PHP & MVC
-
Upload
chris-weldon -
Category
Technology
-
view
22.541 -
download
12
description
Transcript of PHP & MVC
![Page 1: PHP & MVC](https://reader031.fdocuments.net/reader031/viewer/2022013114/548291efb07959380c8b4818/html5/thumbnails/1.jpg)
PHP AND MVC
Daniel Cousineau & Chris WeldonBar Camp Texas 2008
![Page 2: PHP & MVC](https://reader031.fdocuments.net/reader031/viewer/2022013114/548291efb07959380c8b4818/html5/thumbnails/2.jpg)
First Things First
http://www.google.com/search?hl=en&q=mvc
Google is wonderful, learn how to use it.
Or Yahoo
There are 6,203,493* search engines, use one
http://www.toosweettobesour.com/
Slides if anyone wants ‘em
http://www.chrisweldon.net/
* Possibly incorrect
![Page 3: PHP & MVC](https://reader031.fdocuments.net/reader031/viewer/2022013114/548291efb07959380c8b4818/html5/thumbnails/3.jpg)
MVC Is…
Model, View, Controller
Object Oriented
Is communication between Model, View, and Controller Objects
Software Engineering
Organization
RAD
Code reuse!
![Page 4: PHP & MVC](https://reader031.fdocuments.net/reader031/viewer/2022013114/548291efb07959380c8b4818/html5/thumbnails/4.jpg)
History
![Page 5: PHP & MVC](https://reader031.fdocuments.net/reader031/viewer/2022013114/548291efb07959380c8b4818/html5/thumbnails/5.jpg)
MVC Is Not New!
Smalltalk-80 (circa 1980) MVC first discussed in 1979 by Trygve Mikkjel
Heyerdahl Reenskaug
MFC (Document/View)
Java’s Swing
QT4 (KDE)
Apple’s Cocoa (Core Data)
Notice a trend? MVC is perfectly suited for GUI development…
![Page 6: PHP & MVC](https://reader031.fdocuments.net/reader031/viewer/2022013114/548291efb07959380c8b4818/html5/thumbnails/6.jpg)
MVC And The Web
Made popular by Ruby on Rails A good number of PHP MVC frameworks are Rails
inspired
Wikipedia currently lists about 17 PHP MVC frameworks CakePHP
Symfony
Code Igniter
Zend Framework
Make your own (I did!)
![Page 7: PHP & MVC](https://reader031.fdocuments.net/reader031/viewer/2022013114/548291efb07959380c8b4818/html5/thumbnails/7.jpg)
Model – View – Controller
Define MVC
![Page 8: PHP & MVC](https://reader031.fdocuments.net/reader031/viewer/2022013114/548291efb07959380c8b4818/html5/thumbnails/8.jpg)
Model
Wikipedia:
“The domain-specific representation of the information that the application operates.”
Data Storage/Access
Often Data source backed
MySQL, MSSQL, Web Service, it doesn’t matter…
Abstraction, Abstraction, Abstraction!
![Page 9: PHP & MVC](https://reader031.fdocuments.net/reader031/viewer/2022013114/548291efb07959380c8b4818/html5/thumbnails/9.jpg)
View
Wikipedia: “Renders the model into a form suitable for
interaction, typically a user interface element. Multiple views can exist for a single model for different purposes.”
HTML Templates All formatting related code belongs here
A Smarty object is a good example Any template engine works, however…
Abstraction, Abstraction, Abstraction!
![Page 10: PHP & MVC](https://reader031.fdocuments.net/reader031/viewer/2022013114/548291efb07959380c8b4818/html5/thumbnails/10.jpg)
Controller
Wikipedia: “Processes and responds to events, typically user
actions, and may invoke changes on the model.”
A BIG DEAL Process user inputs, communicate with Models and
Views The Go-Between
Much of the application’s core logic
Utilize the abstraction of before! Invoke model, assign values to views
![Page 11: PHP & MVC](https://reader031.fdocuments.net/reader031/viewer/2022013114/548291efb07959380c8b4818/html5/thumbnails/11.jpg)
Putting It Together…
http://ash-mvc.org/website/framework/framework.html
![Page 12: PHP & MVC](https://reader031.fdocuments.net/reader031/viewer/2022013114/548291efb07959380c8b4818/html5/thumbnails/12.jpg)
As A Framework
![Page 13: PHP & MVC](https://reader031.fdocuments.net/reader031/viewer/2022013114/548291efb07959380c8b4818/html5/thumbnails/13.jpg)
Common Traits
index.php
Parses the URL
Often mod_rewrite used:
/controller/method/arg0/value0/arg1/…/valuen
Initializes proper controller
Executes correct method
Facilitates communication from controller to view
Signals view to render
![Page 14: PHP & MVC](https://reader031.fdocuments.net/reader031/viewer/2022013114/548291efb07959380c8b4818/html5/thumbnails/14.jpg)
Directory Structure
Outside of web root. More secure! (generally) config/ controllers/ models/ views/
Usually not PHP files
includes/ Any extra libraries, e.g. PHPMailer
framework/ Framework system files (do not have to reside in
application)
![Page 15: PHP & MVC](https://reader031.fdocuments.net/reader031/viewer/2022013114/548291efb07959380c8b4818/html5/thumbnails/15.jpg)
Helpers and Plug-ins
Usually common functions used by Views E.g. format_phone(), create_calendar(),
etc.
Some loaded through index.php, some loaded specifically by controller
Do not allow direct manipulation of models or controllers
Any code requiring access to models should be in the controller, not view
![Page 16: PHP & MVC](https://reader031.fdocuments.net/reader031/viewer/2022013114/548291efb07959380c8b4818/html5/thumbnails/16.jpg)
Other Utilities…
Dispatcher classes
Abstract the location of files
Abstract creation of links/connections
Caching classes
Cache Templates? Output? SQL Queries? Opcode
Caching individual Templates poses a special problem
Will dynamism of framework affect caching?
![Page 17: PHP & MVC](https://reader031.fdocuments.net/reader031/viewer/2022013114/548291efb07959380c8b4818/html5/thumbnails/17.jpg)
Why MVC?
Good architectural design
Code is organized and structure
Code structure lends itself to an easy to understand directory structure
Easy code maintenance
Because of abstraction, better strategic positioning of code will minimize the hunt for places to change
Easy to extend and grow
Modify parent classes, drop in new controller, etc.
![Page 18: PHP & MVC](https://reader031.fdocuments.net/reader031/viewer/2022013114/548291efb07959380c8b4818/html5/thumbnails/18.jpg)
Sites Using PHP MVC Frameworks
CakePHP
Mozilla Addons
The Onion’s Online Store
Symfony
TED.com
Yahoo! Bookmarks
Pretty much everyone.
![Page 19: PHP & MVC](https://reader031.fdocuments.net/reader031/viewer/2022013114/548291efb07959380c8b4818/html5/thumbnails/19.jpg)
The Cake Was NOT a Lie!