Lecture 3- MVC and testing
Lecture 3- MVC and testing
Chapter 5 and 6
Dr. Rasha Montaser
LOGO Home About Service Contact
Week 3 Outcomes
• Distinguish between Model 1 and Model 2
architecture web applications
• Employ the Model-View-Controller design
pattern in web development
• Utilize RESTful URLs for clean design
• List the advantages to unit testing
LOGO Home About Service Contact
Web Architecture
• Model 1 architecture
– Code for application, database, and display
logic is intermixed in a single monolithic page
– A very tangled set of interactions.
• Ex: To do application
3
LOGO Home About Service Contact
Web Architecture
• Model 1 architecture
– Works fine for simple applications
– Becomes horribly messy when
• Application logic is complicated
• Error/security reporting is robust
• You need to debug or make changes
– Problem: solving three issues in one place.
– Solution: separate three issues into three
places.
4
LOGO Home About Service Contact
MVC-based Architecture
• Model 2:
– Model-View-Controller (MVC)
• Model: interacts with the database (php only no
html)
• View: presents data to the user (html with some php
to display dynamic data)
• Controller: encapsulates “business logic”, receives
the http request from the browser and get the
appropriate data from the model and return the
appropriate views to the user (php only)
5
LOGO Home About Service Contact
MVC-based Architecture
DB
• Browser sends a request to a
web server
Model View • Web server fires a routing
script to determine what other
script (based on URL) should
Controller
handle the request.
• Routing script dispatches the
Dispatching request to the appropriate
Routing
Web Server controller.
• Controller invokes data-
oriented actions on the model
Browser
7
LOGO Home About Service Contact
MVC-based Architecture
DB
• Model updates/queries the
database
Model View
• Model returns results to the
controller.
• Controller takes results,
Controller applies business logic, and
forwards results to the view.
• View renders the HTML and
Dispatching
Routing returns it to the controller.
Web Server • Controller returns HTML to
web server and then to
browser.
Browser
8
LOGO Home About Service Contact
MVC-based Architecture
• We’ve already done some of this last
week:
– Separation of DB instantiation into db.inc
– Separation of model into model_student.inc
– Separation of view into view_student.php
• But, we need to structure this better!
9
LOGO Home About Service Contact
Redirect request
• If a PHP file needs to run itself again, you can’t use the
include function to forward the request.
• You can use the header function to redirect the request
to the same file.
LOGO Home About Service Contact
Using MVC
• Model
– Database.php → get connection to the database
– Category_db.php → works with category data file
– Product_db.php → works with product data file
• View
– Product_list.php → user view and delete product
– Product_add.php→ user view add product
– Database_error.php→ display error page
• Controller
– Index.php→ the default file of the application
directory, called when the user starts the application
LOGO Home About Service Contact
DB
Controller
Dispatching
Routing
Web Server
Browser
LOGO Home About Service Contact
The controller
DB
Model View
Controller
Dispatching
Routing
Web Server
Browser
LOGO Home About Service Contact
Index.php
Import the Model files
Index.php cont.
Make sure that the page
required to be displayed is
product list
Note
• In a real word application you need to add complete
data validation.
LOGO Home About Service Contact
The View
DB
Model View
Controller
Dispatching
Routing
Web Server
Browser
LOGO Home About Service Contact
Product_list.php
Call the header
Display list of
categories
LOGO Home About Service Contact
Product_list.php cont.
Product_add.php
Call header
Call footer
LOGO Home About Service Contact
Test phases
1. Check the user interface to make sure that it works
correctly. (check controls, check keys)
2. Test the application with valid input data to make sure
the results are correct.
3. Test the results with invalid data or unexpected
actions.
LOGO Home About Service Contact
3
1
LOGO Home About Service Contact
3
4
LOGO Home About Service Contact
3
5
LOGO Home About Service Contact
3
7
LOGO Home About Service Contact
Upcoming Deadlines
• Readings for next week
– Chapters 7 and 8 in PHP and MySQL
• Next week:
– Forms and data, control statements