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

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Resources

�  http://kohanaframework.org/

�  http://kohanaframework.org/3.3/guide

�  http://kohanaframework.org/3.3/guide-api