Organize your code with MVC

Slides:



Advertisements
Similar presentations
Murach's PHP and MySQL, C2© 2010, Mike Murach & Associates, Inc.Slide 1.
Advertisements

Murach's MySQL, C2© 2012, Mike Murach & Associates, Inc.Slide 1.
Murach's PHP and MySQL, C2© 2010, Mike Murach & Associates, Inc.Slide 1.
Introduction to PHP MIS 3501, Fall 2014 Jeremy Shafer
Murach's PHP and MySQL, C4© 2010, Mike Murach & Associates, Inc.Slide 1.
Murach's PHP and MySQL, C4© 2010, Mike Murach & Associates, Inc.Slide 1.
Class03 Introduction to Web Development with PHP MIS 3501, Fall 2015 Brad Greenwood, PhD MBA Department of MIS Fox School of Business Temple University.
CS 4720 Model-View-Controller CS 4720 – Web & Mobile Systems.
Unit 1 – Web Concepts Instructor: Brent Presley.
Class11 Introduction to relational databases and MySQL MIS 3501, Fall 2015 Brad Greenwood, PhD MBA Department of MIS Fox School of Business Temple University.
Class06 Conditional Statements MIS 3501, Fall 2015 Brad Greenwood, PhD MBA Department of MIS Fox School of Business Temple University 9/10/2015 © 2014,
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
Class03 Introduction to Web Development (Hierarchy and the IDE) MIS 3501, Spring 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University.
Introduction to JavaScript MIS 3502, Spring 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 2/2/2016.
Responsive Web Design (RWD) MIS 3502, Spring 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 2/11/2016.
Sessions and cookies (part 2) MIS 3501, Fall 2015 Brad N Greenwood, PhD Department of MIS Fox School of Business Temple University 11/19/2015.
Form Data (part 2) MIS 3502, Fall 2015 Brad N Greenwood, PhD Department of MIS Fox School of Business Temple University 11/10/2015 Slide 1.
Form Data (part 1) MIS 3502, Fall 2015 Brad Greenwood, PhD Department of MIS Fox School of Business Temple University 11/10/2015.
Class07 PHP: loops and includes MIS 3501, Fall 2015 Brad Greenwood, PhD MBA Department of MIS Fox School of Business Temple University 9/15/2015.
Class05 How to get data from a form MIS 3501, Fall 2015 Brad N Greenwood, PhD MBA Department of MIS Fox School of Business Temple University 9/8/2015.
Sessions and cookies MIS 3501, Spring 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 4/12/2016.
PDO Database Connections MIS 3501, Fall 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 3/8/2016.
Introduction to JavaScript MIS 3502, Fall 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 9/29/2016.
PDO Database Connections
© 2010, Mike Murach & Associates, Inc.
Class03 Introduction to Web Development (Hierarchy and the IDE)
Brad N Greenwood, PhD MBA
Sessions and cookies MIS 3501 Jeremy Shafer Department of MIS
Form Data (part 2) MIS 3502, Fall 2015 Jeremy Shafer Department of MIS
Introduction to Web Development (Part 2)
PHP: includes MIS 3501 Jeremy Shafer Department of MIS
Sessions and cookies (part 2)
Form Data (part 1) MIS 3502, Fall 2015 Jeremy Shafer Department of MIS
Class07 PHP: loops and includes
PDO Database Connections
How to get data from a form
© 2010, Mike Murach & Associates, Inc.
© 2010, Mike Murach & Associates, Inc.
Introduction to JavaScript
MVC Framework, in general.
PDO Database Connections: Getting data out of the database
© 2010, Mike Murach & Associates, Inc.
Form Data (part 2) MIS 3501 Jeremy Shafer Department of MIS
A second look at JavaScript
PDO Database Connections
Organize your code with MVC
© 2010, Mike Murach & Associates, Inc.
© 2010, Mike Murach & Associates, Inc.
Introduction to relational databases and MySQL
Sessions and cookies (part 1)
PDO Database Connections
© 2015, Mike Murach & Associates, Inc.
© 2015, Mike Murach & Associates, Inc.
© 2015, Mike Murach & Associates, Inc.
Form Data (part 2) MIS 3501 Jeremy Shafer Department of MIS
Class07 PHP: loops MIS 3501 Jeremy Shafer Department of MIS
Class08 Using the Documentation
Class05 How to get data from a form
MySQL Backup, Transfer and Restore
Sessions and cookies MIS 3501 Jeremy Shafer Department of MIS
Class11 Introduction to relational databases and MySQL
Tutorial 6 PHP & MySQL Li Xu
Form Data (part 1) MIS3501 Jeremy Shafer Department of MIS
Introduction to JavaScript
© 2015, Mike Murach & Associates, Inc.
ASP.NET MVC Web Development
MVC – Model View Controller
© 2015, Mike Murach & Associates, Inc.
© 2010, Mike Murach & Associates, Inc.
Presentation transcript:

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

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

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)

© 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.

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

© 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.

© 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.

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

© 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.

© 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.

© 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.

© 2014, Mike Murach & Associates, Inc.

© 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.

© 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.

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

© 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.

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

© 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.

© 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.

© 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.

© 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.

© 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.

© 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.

© 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.

© 2014, Mike Murach & Associates, Inc.

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

© 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.

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.

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.

© 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.

© 2014, Mike Murach & Associates, Inc.