Presentation is loading. Please wait.

Presentation is loading. Please wait.

Organize your code with MVC

Similar presentations


Presentation on theme: "Organize your code with MVC"— Presentation transcript:

1 Organize your code with MVC
MIS 3502, Fall 2015 Brad Greenwood, PhD Department of MIS Fox School of Business Temple University 10/22/2015

2 © 2014, Mike Murach & Associates, Inc.
What Have We Done © 2014, Mike Murach & Associates, Inc.

3 Course Overview We are here. To do: MySQL
Weeks 6 & 7 HTML & CSS Weeks 1 & 2 PHP Weeks 3 – 5 PDO Week 8 & 9 To do: Organize your code with MVC (week 10) Work with forms (week 11) Use cookies and sessions (week 12)

4 © 2014, Mike Murach & Associates, Inc.
Objectives Understand what the MVC pattern is. Understand how the MVC pattern can be used to develop web applications (what files go where, and why) Understand how functions can be used to perform database processing. © 2014, Mike Murach & Associates, Inc.

5 Why do we need the MVC pattern?
© 2014, Mike Murach & Associates, Inc.

6 © 2014, Mike Murach & Associates, Inc.
The MVC pattern The Controller: Act like a traffic cop… receive user input and decide what to do. © 2014, Mike Murach & Associates, Inc.

7 © 2014, Mike Murach & Associates, Inc.
The MVC pattern The Model: talk to the database. Enforce rules regarding what is / is not valid data. © 2014, Mike Murach & Associates, Inc.

8 © 2014, Mike Murach & Associates, Inc.
The MVC pattern The View: specifies what the user can see. © 2014, Mike Murach & Associates, Inc.

9 © 2014, Mike Murach & Associates, Inc.
The MVC pattern SANITY CHECK! – In which of the above would you expect to see code that uses PDO syntax? © 2014, Mike Murach & Associates, Inc.

10 © 2014, Mike Murach & Associates, Inc.
The MVC pattern SANITY CHECK! – In which of the above would you expect to see a lot of HTML? © 2014, Mike Murach & Associates, Inc.

11 © 2014, Mike Murach & Associates, Inc.
The MVC pattern SANITY CHECK! – In which of the above would you expect to see a lot of if / else statements? © 2014, Mike Murach & Associates, Inc.

12 © 2014, Mike Murach & Associates, Inc.

13 © 2014, Mike Murach & Associates, Inc.
Where do the files go? Up until now, you have been organizing your code like this. © 2014, Mike Murach & Associates, Inc.

14 © 2014, Mike Murach & Associates, Inc.
Where do the files go? Now we are going to start using folders to manage our php files. © 2014, Mike Murach & Associates, Inc.

15 © 2014, Mike Murach & Associates, Inc.
Where do the files go? Collectively, this is all a web site. © 2014, Mike Murach & Associates, Inc.

16 © 2014, Mike Murach & Associates, Inc.
Where do the files go? This is the home page (or landing page) for the site. The single main.css file is used consistenly throughout the site. © 2014, Mike Murach & Associates, Inc.

17 © 2014, Mike Murach & Associates, Inc.
Where do the files go? Inside the site, there are two applications. © 2014, Mike Murach & Associates, Inc.

18 © 2014, Mike Murach & Associates, Inc.
Where do the files go? These folders contain model and view files that will be used on the entire site. © 2014, Mike Murach & Associates, Inc.

19 © 2014, Mike Murach & Associates, Inc.
Where do the files go? These folders contain additional supporting files used by both the catalog and manager applications. © 2014, Mike Murach & Associates, Inc.

20 © 2014, Mike Murach & Associates, Inc.
Where do the files go? The cart folder is not yet in use. (You may ignore this for now.) © 2014, Mike Murach & Associates, Inc.

21 © 2014, Mike Murach & Associates, Inc.
Why are we doing this? There are three reasons: Avoid repeated code. This concept is commonly referred to as DRY (Don’t Repeat Yourself) © 2014, Mike Murach & Associates, Inc.

22 © 2014, Mike Murach & Associates, Inc.
What’s in here???? Inside the product_catalog application we have index.php. In this context, index.php is the controller. © 2014, Mike Murach & Associates, Inc.

23 © 2014, Mike Murach & Associates, Inc.
What’s in here???? We also have these views. Views are named here with the convention of entity-underscore-verb. © 2014, Mike Murach & Associates, Inc.

24 © 2014, Mike Murach & Associates, Inc.
SANITY CHECK - If I were to see a view named product_add.php, what sort of operation would that page be used for? SANITY CHECK – Where would I expect to see the code that connects to the database and adds the product information? The same place (the view) or someplace else? © 2014, Mike Murach & Associates, Inc.

25 © 2014, Mike Murach & Associates, Inc.

26 Getting data out of the database
© 2014, Mike Murach & Associates, Inc.

27 © 2014, Mike Murach & Associates, Inc.
Putting data in Notice that this function doesn’t return a value. It has no return statement. It doesn’t really need one. But, what might be a reasonable use of a returned result in this operation? © 2014, Mike Murach & Associates, Inc.

28 What you will see in the controller.
Notice that this code is much lighter (more simple) than what the model is doing. Remember the controller is just directing traffic. It should defer “the hard stuff” like data manipulation to the Model files, and data presentation to the View files. © 2014, Mike Murach & Associates, Inc.

29 Setting expectations…
So that’s it, right? Every MVC application is structured exactly like this. Right? Actually no. There are a lot of variations on the MVC pattern. For instance, some developers will put all the controller code in a folder named “controllers”, and others will have lively debates over where form validation should go. The important thing is to be consistent. But now you have a whole new vocabulary for developing web applications. This vocabulary will help make every implementation of MVC more sensible. © 2014, Mike Murach & Associates, Inc.

30 © 2014, Mike Murach & Associates, Inc.
We call the process of changing the file structure of a web site or a web application, without changing its appearance or behavior: refactoring. © 2014, Mike Murach & Associates, Inc.

31 © 2014, Mike Murach & Associates, Inc.


Download ppt "Organize your code with MVC"

Similar presentations


Ads by Google