Bài giảng Web technologies and e-Services - Bài 4, Phần 2: MVC and PHP Frameworks - Nguyễn Bình Minh
Web Development
MVC & PHP Frameworks
1
u Everything shoved into one file. L Not Good!
<?
$link = mysql_connect('localhost', 'myuser', 'mypass');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
Typical PHP Code
if($submit) {
$sql = “INSERT INTO my_table (name,address,city,state,zip)
VALUES (”;
$sql .= “’$name’,’$address’,’$city’,’$state’,’$zip’)”;
mysql_query($sql);
} else {
$result = mysql_query(“SELECT * FROM my_table WHERE id = 1”);
$userArray = mysql_fetch_array($result);
} ?>
<html>
<head><title>Add User</title></head>
<body>
<div>My HTML code blah blah</div>
<form method=“POST”>
Name: <input type=“text” name=“name”
value=“<?=$userArray[‘name’]?>”><br>
…
</form>
…
2
2
1
<?
require_once(“config.inc.php");
require_once(“database.inc.php");
$dbh = dbConnect();
if($submit) {
Better but still not great
$sql = “INSERT INTO my_table
(name,address,city,state,zip) VALUES (”;
$sql .= “’$name’,’$address’,’$city’,’$state’,’$zip’)”;
$db>query($sql);
} else {
$result = $db>query(“SELECT * FROM my_table”);
$userArray = $db>fetchRow($result);
}
printHeader();?>
<div>My HTML code blah blah</div>
<form method=“POST”>
Name: <input type=“text” name=“name”
value=“<?=$userArray[‘name’]?>”><br>
…
</form>
…
3
<? printFooter(); ?>
3
Content
2What is MVC architecture?
3PHP Frameworks
4
4
2
Patterns in Architecture
u Does this room
makes you feel
happy?
u Why?
–Light (direction)
–Proportions
–Symmetry
–Furniture
–And more…
5
What is a Design Pattern?
A description of a recurrent problem
and of the core of possible solutions.
In Short, a solution
for a typical problem
6
3
Why do we need Patterns?
u Reusing design knowledge
– Problems are not always unique. Reusing
existing experience might be useful.
– Patterns give us hints to “where to look for
problems”.
7
History of Design Patterns
Christopher Alexander
Architecture
1970’
ss of Buildi
A Pattern Language: Towns, Buildings, Construction
Gang of Four (GoF)
Object Oriented
atr
Reusable ObjeOriented Software
1995’
Other Areas:
Many Authors
zl
2007’
GoF: Gamma et al (E. Gamma, R. Helm, R. Johnson, J. Vlissides)
8
4
Content
1Overview of Design Pattern
3PHP Frameworks
9
9
1. What is MVC Architecture?
u MVC is a design structure for separating
representation from presentation using a
subscribe/notify protocol
u The basic idea is to separate
– where and how data (or more generally some
state) is stored, i.e., the model
– from how it is presented, i.e., the views
u Follows basic software engineering
principles:
– Separation of concerns
– Abstraction
10
5
1. What is MVC Architecture? (2)
u MVC consists of three kinds of objects
– Model is the application object
– View is its screen presentation
– Controller defines the way the user interface
reacts to user input
11
1. What is MVC Architecture? (3)
u MVC decouples views and models by
establishing a subscribe/notify protocol
between them
– whenever model changes it notifies the views
that depend on it
– in response each view gets an opportunity to
update itself
u This architecture allows you to attach
multiple views to a model
– it is possible to create new views for a model
without rewriting it
12
6
MVC Architecture in Web
Applications
u Many web frameworks support web
application development based on the
MVC architecture
– Ruby on Rails, Zend Framework for PHP,
CakePHP, Spring Framework for Java, Struts
Framework for Java, Django for Python, …
u MVC architecture has become the
standard way to structure web
applications
13
MVC Framework for Web
Applications
u ModeViewController
u Separates:
– M: Data model
– V: Presentation (UI)
– C: Business logic
14
7
MVC Framework for Web
Applications
u Model: Data model which is an abstract
representation of the data stored in the backend
database. Typically uses an objecrelational
mapping to map the class structure for the data
model to the tables in the bacsend database
u Views: These are responsible for rendering of the
web pages, i.e., how is the data presented in
user’s browser
u Controllers: Controllers are basically event
handlers that process incoming user requests.
Based on a user request, they can update the
data model, and create a new view to be
presented to the user
15
Why use an MVC framework?
Avoid “reinventing the wheel”
Use proven, tested code
Automation (ORM, generators)
Maintainability
“Plugin” functionality
16
8
Flow: Traditional vs. MVC
Query
Processing
Output
Model
Output
Controller
Query
Output
Processing
View
Final Output
17
Content
1Overview of Design Patterns
2What is MVC architecture?
18
18
9
3.1. Your own framework
19
19
3.1. Your own framework (2)
20
10
3.1. Your own framework (2)
21
21
3.2. Existed PHP Frameworks
22
22
11
Popular PHP MVC Frameworks
u CakePHP
– Documentation is somewhat lacking
– Apparently difficult for beginners
u Symfony
– Great documentation and community
– Easy to get started
u Zend
– Supported by Zend (official PHP company)
– More of a library than complete framework
23
Should you use an existed MVC
framework for your project?
u Are there complex hierarchical
relationships in your data?
u Will this project need to be maintained by
more than one person for more than a
year?
u Do you need the ability to add advanced
features like AJAX without writing the code
from scratc?
u Probably Yes. (unless it’s a throwaway)
– Use a weestablished framework with good
documentation and a large community
u But I ask to NOT USE any framework
24
12
Question?
25
25
13
Bạn đang xem tài liệu "Bài giảng Web technologies and e-Services - Bài 4, Phần 2: MVC and PHP Frameworks - Nguyễn Bình Minh", để tải tài liệu gốc về máy hãy click vào nút Download ở trên
File đính kèm:
- bai_giang_web_technologies_and_e_services_bai_4_phan_2_mvc_a.pdf