What is a framework?cdn.cs50.net/2012/fall/seminars/kohana/kohana.pdfAn architecture for your...
Transcript of What is a framework?cdn.cs50.net/2012/fall/seminars/kohana/kohana.pdfAn architecture for your...
![Page 1: What is a framework?cdn.cs50.net/2012/fall/seminars/kohana/kohana.pdfAn architecture for your project (e.g. MVC) ! Embodies a set of procedures, rules, and practices ! “Inversion](https://reader036.fdokument.com/reader036/viewer/2022070822/5f296fb7dd3c775e55532d32/html5/thumbnails/1.jpg)
![Page 2: What is a framework?cdn.cs50.net/2012/fall/seminars/kohana/kohana.pdfAn architecture for your project (e.g. MVC) ! Embodies a set of procedures, rules, and practices ! “Inversion](https://reader036.fdokument.com/reader036/viewer/2022070822/5f296fb7dd3c775e55532d32/html5/thumbnails/2.jpg)
What is a framework?
� Allows you to write higher-level code without worrying about lower-level details
� Libraries and utilities to eliminate boilerplate code
� An architecture for your project (e.g. MVC)
� Embodies a set of procedures, rules, and practices
� “Inversion of control”
![Page 3: What is a framework?cdn.cs50.net/2012/fall/seminars/kohana/kohana.pdfAn architecture for your project (e.g. MVC) ! Embodies a set of procedures, rules, and practices ! “Inversion](https://reader036.fdokument.com/reader036/viewer/2022070822/5f296fb7dd3c775e55532d32/html5/thumbnails/3.jpg)
Kohana PHP Framework!
![Page 4: What is a framework?cdn.cs50.net/2012/fall/seminars/kohana/kohana.pdfAn architecture for your project (e.g. MVC) ! Embodies a set of procedures, rules, and practices ! “Inversion](https://reader036.fdokument.com/reader036/viewer/2022070822/5f296fb7dd3c775e55532d32/html5/thumbnails/4.jpg)
Kohana
� One of many PHP frameworks
� Lightweight, gentle learning curve
� Uses MVC architecture
![Page 5: What is a framework?cdn.cs50.net/2012/fall/seminars/kohana/kohana.pdfAn architecture for your project (e.g. MVC) ! Embodies a set of procedures, rules, and practices ! “Inversion](https://reader036.fdokument.com/reader036/viewer/2022070822/5f296fb7dd3c775e55532d32/html5/thumbnails/5.jpg)
MVC Architecture
View
Controller
Model
MySQL Database
Client/Browser
![Page 6: What is a framework?cdn.cs50.net/2012/fall/seminars/kohana/kohana.pdfAn architecture for your project (e.g. MVC) ! Embodies a set of procedures, rules, and practices ! “Inversion](https://reader036.fdokument.com/reader036/viewer/2022070822/5f296fb7dd3c775e55532d32/html5/thumbnails/6.jpg)
MVC Components
� Model: Database interface and application state
� Controller: Application logic
� View: Render the data using a template
� Why? � Separation of concerns
� Code reuse
� Don’t Repeat Yourself (DRY) code
![Page 7: What is a framework?cdn.cs50.net/2012/fall/seminars/kohana/kohana.pdfAn architecture for your project (e.g. MVC) ! Embodies a set of procedures, rules, and practices ! “Inversion](https://reader036.fdokument.com/reader036/viewer/2022070822/5f296fb7dd3c775e55532d32/html5/thumbnails/7.jpg)
Let’s try it!
� We’re going to build a very basic blog from scratch!
![Page 8: What is a framework?cdn.cs50.net/2012/fall/seminars/kohana/kohana.pdfAn architecture for your project (e.g. MVC) ! Embodies a set of procedures, rules, and practices ! “Inversion](https://reader036.fdokument.com/reader036/viewer/2022070822/5f296fb7dd3c775e55532d32/html5/thumbnails/8.jpg)
Routing
� Matches URLs to controller actions
� foo.com/blog/all will be routed to Controller_Blog#action_all
� URL parts can also be mapped to action parameters!
![Page 9: What is a framework?cdn.cs50.net/2012/fall/seminars/kohana/kohana.pdfAn architecture for your project (e.g. MVC) ! Embodies a set of procedures, rules, and practices ! “Inversion](https://reader036.fdokument.com/reader036/viewer/2022070822/5f296fb7dd3c775e55532d32/html5/thumbnails/9.jpg)
Controllers
� Controllers handle application logic
� Controllers have various actions that handle different things
� Create separate controllers for separate parts of website (generally one controller per model)
![Page 10: What is a framework?cdn.cs50.net/2012/fall/seminars/kohana/kohana.pdfAn architecture for your project (e.g. MVC) ! Embodies a set of procedures, rules, and practices ! “Inversion](https://reader036.fdokument.com/reader036/viewer/2022070822/5f296fb7dd3c775e55532d32/html5/thumbnails/10.jpg)
Object-Relational Mapping
� Representing database rows as programming objects
� Preserves relationships between objects
� Allows you to attach functionality to objects
![Page 11: What is a framework?cdn.cs50.net/2012/fall/seminars/kohana/kohana.pdfAn architecture for your project (e.g. MVC) ! Embodies a set of procedures, rules, and practices ! “Inversion](https://reader036.fdokument.com/reader036/viewer/2022070822/5f296fb7dd3c775e55532d32/html5/thumbnails/11.jpg)
Post Model
� Automatically comes with a lot of pre-built functions!
� Can attach additional functions (highly suggested)
� Where validations and filtering will happen
![Page 12: What is a framework?cdn.cs50.net/2012/fall/seminars/kohana/kohana.pdfAn architecture for your project (e.g. MVC) ! Embodies a set of procedures, rules, and practices ! “Inversion](https://reader036.fdokument.com/reader036/viewer/2022070822/5f296fb7dd3c775e55532d32/html5/thumbnails/12.jpg)
Action for all posts
� We need to: � Grab a list of all the posts
� Render a list of all the posts
![Page 13: What is a framework?cdn.cs50.net/2012/fall/seminars/kohana/kohana.pdfAn architecture for your project (e.g. MVC) ! Embodies a set of procedures, rules, and practices ! “Inversion](https://reader036.fdokument.com/reader036/viewer/2022070822/5f296fb7dd3c775e55532d32/html5/thumbnails/13.jpg)
Views
� Dynamic views for generating different pages from a single template
� Separates view code from the rest of the app
� Plenty of helper functions to help you write code faster!
![Page 14: What is a framework?cdn.cs50.net/2012/fall/seminars/kohana/kohana.pdfAn architecture for your project (e.g. MVC) ! Embodies a set of procedures, rules, and practices ! “Inversion](https://reader036.fdokument.com/reader036/viewer/2022070822/5f296fb7dd3c775e55532d32/html5/thumbnails/14.jpg)
Action for creating new posts
� We need to: � Create a form
� Check whether we’re making a POST request
� Create a Post object
� Attempt to save it
� Display errors if there are any, and keep the old values
� Redirect to blog if all goes well
![Page 15: What is a framework?cdn.cs50.net/2012/fall/seminars/kohana/kohana.pdfAn architecture for your project (e.g. MVC) ! Embodies a set of procedures, rules, and practices ! “Inversion](https://reader036.fdokument.com/reader036/viewer/2022070822/5f296fb7dd3c775e55532d32/html5/thumbnails/15.jpg)
Templates
Add a template for your entire site!
![Page 16: What is a framework?cdn.cs50.net/2012/fall/seminars/kohana/kohana.pdfAn architecture for your project (e.g. MVC) ! Embodies a set of procedures, rules, and practices ! “Inversion](https://reader036.fdokument.com/reader036/viewer/2022070822/5f296fb7dd3c775e55532d32/html5/thumbnails/16.jpg)
XSS Security
One last thing to be careful of J
![Page 17: What is a framework?cdn.cs50.net/2012/fall/seminars/kohana/kohana.pdfAn architecture for your project (e.g. MVC) ! Embodies a set of procedures, rules, and practices ! “Inversion](https://reader036.fdokument.com/reader036/viewer/2022070822/5f296fb7dd3c775e55532d32/html5/thumbnails/17.jpg)
Resources
� http://kohanaframework.org/
� http://kohanaframework.org/3.3/guide
� http://kohanaframework.org/3.3/guide-api