Difference between revisions of "Web Frameworks"

From CSE330 Wiki
Jump to: navigation, search
(Created page with 'So far in this class, you have been using PHP to run your web site. You may have found that PHP can become burdensome when you are writing complex applications, and it can be ha…')
 
Line 6: Line 6:
  
 
== Common Features of Web Frameworks ==
 
== Common Features of Web Frameworks ==
 +
 +
=== Less Code ===
 +
 +
Frameworks are designed to separate your application logic from your HTML and CSS.  In fact, most web frameworks automatically generate all of the HTML and CSS that you need.  So, you could write a fully functional (albeit bland-looking) web site in a Web Framework without writing a singly line of HTML or CSS!
  
 
=== Object-Relational Mapping ===
 
=== Object-Relational Mapping ===
Line 37: Line 41:
 
# The controller passes the information from the models to a view.
 
# The controller passes the information from the models to a view.
 
# The final view is sent to the user.
 
# The final view is sent to the user.
 +
 +
=== Routing ===
 +
 +
A key difference between PHP and web frameworks is in how routing occurs.
 +
 +
In PHP, if you went to an address like http://www.example.com/welcome.php then Apache would run the script located at, for example, ''/var/www/html/welcome.php''.
 +
 +
However, frameworks do ''not'' generally work the same way.  This is in part because of the MVC architecture.  Most frameworks use a ''router'' instead that routes arbitrary URLs to controllers.  Scripts are never "loaded" directly through Apache.
 +
 +
Here is a sample router file from a Ruby on Rails web application so you can get an idea of how it works:
 +
 +
<source lang="ruby">
 +
MovieNews::Application.routes.draw do
 +
  resource :session, only: [:new, :create, :destroy]
 +
 +
  resources :users
 +
 +
  resources :movies do
 +
    resources :reviews
 +
  end
 +
 
 +
  root to: "movies#index"
 +
  get "released-movies" => "movies#index", as: "released_movies"
 +
  get "signup" => "users#new", as: "signup"
 +
  get "login" => "sessions#new", as: "login"
 +
  get "logout" => "sessions#destroy", as: "logout"
 +
end
 +
</source>
  
 
== Some Popular Web Frameworks ==
 
== Some Popular Web Frameworks ==
Line 48: Line 80:
 
Rails is great for web sites that follow a typical web design pattern: for example, blogs, e-commerce, and news sites.
 
Rails is great for web sites that follow a typical web design pattern: for example, blogs, e-commerce, and news sites.
  
=== Ruby on Rails Resources ===
+
==== Ruby on Rails Resources ====
 +
 
 +
* [http://rubyonrails.org/ Official Web Site]
 +
* [http://guides.rubyonrails.org/ Guides and Tutorials]
 +
* [http://api.rubyonrails.org/ Rails Documentation]
 +
* [http://railscasts.com/ Rails Screencasts]
 +
* [http://www.ruby-lang.org/en/documentation/quickstart/ Ruby Tutorial]
 +
 
 +
==== Notable Sites using Ruby on Rails ====
 +
 
 +
* [http://twitter.com/ Twitter]
 +
* [https://github.com/ Github]
 +
* [http://www.groupon.com/ Groupon]
 +
* [http://www.yellowpages.com/ YellowPages]
 +
 
 +
=== Django ===
 +
 
 +
Django is an open source web development framework that uses Python as its programming language.
 +
 
 +
Django uses object-relational mapping for interacting with the database.  It also uses an MVC architecture; note that Django calls the Controller the ''view'' and the View the ''template''.
 +
 
 +
Because of Python's large following outside of the realm of web development, there are more general-use libraries available for Python than there are for a language like Ruby.  As such, Django is great for sites that require complex server-side operations.
 +
 
 +
==== Django Resources ====
 +
 
 +
* [https://www.djangoproject.com/ Official Web Site]
 +
* [https://docs.djangoproject.com/en/1.4/ Django Documentation]
 +
 
 +
==== Notable Sites using Django ====
  
=== Notable Sites using Ruby on Rails ===
+
* [http://www.washingtontimes.com/ The Washington Times]
 +
* [http://www.mozilla.org/ Mozilla]
 +
* [http://www.pbs.org/ PBS]
 +
* [http://instagram.com/ Instagram]
  
 
== Other Web Frameworks ==
 
== Other Web Frameworks ==

Revision as of 02:26, 25 August 2012

So far in this class, you have been using PHP to run your web site. You may have found that PHP can become burdensome when you are writing complex applications, and it can be hard to use for collaboration.

Web Frameworks are an alternative method for writing web applications. Web Frameworks are designed to make the experience for the developer more elegant and increase the possibilities for collaboration.

There are hundreds of Web Frameworks out there. We have chosen three of the more popular frameworks to write about in this article. If you want to use a different framework, there is a section at the end telling you what to look out for.

Common Features of Web Frameworks

Less Code

Frameworks are designed to separate your application logic from your HTML and CSS. In fact, most web frameworks automatically generate all of the HTML and CSS that you need. So, you could write a fully functional (albeit bland-looking) web site in a Web Framework without writing a singly line of HTML or CSS!

Object-Relational Mapping

Many Web Frameworks use an object-relational mapping paradigm for communication with the database. What this means is that instead of writing SQL queries, you call methods on objects instead. For example, the following snippet of code in Ruby on Rails loads a certain user out of the database and changes their nickname:

u = User.find_by_username("alice")
u.update_attributes(
  nickname: "Ali"
)

MVC Architecture

In PHP, your code probably ended up jumbled together in various disorganized places. This is part of the reason why many Web Frameworks choose to use a Model-View-Controller architecture.

A directory structure for an MVC framework might look like the following:

  • app (holds MVC components)
    • models (contains models)
    • controllers (contains controllers)
    • views (contains views)
  • config (holds global server configurations)
  • lib (other assorted libraries)

With an MVC architecture, a request to the server typically follows the following path:

  1. The server routes the request to a certain controller.
  2. The controller interprets the request, loading requested information from the models.
  3. The controller passes the information from the models to a view.
  4. The final view is sent to the user.

Routing

A key difference between PHP and web frameworks is in how routing occurs.

In PHP, if you went to an address like http://www.example.com/welcome.php then Apache would run the script located at, for example, /var/www/html/welcome.php.

However, frameworks do not generally work the same way. This is in part because of the MVC architecture. Most frameworks use a router instead that routes arbitrary URLs to controllers. Scripts are never "loaded" directly through Apache.

Here is a sample router file from a Ruby on Rails web application so you can get an idea of how it works:

MovieNews::Application.routes.draw do
  resource :session, only: [:new, :create, :destroy]

  resources :users

  resources :movies do
    resources :reviews
  end
  
  root to: "movies#index"
  get "released-movies" => "movies#index", as: "released_movies"
  get "signup" => "users#new", as: "signup"
  get "login" => "sessions#new", as: "login"
  get "logout" => "sessions#destroy", as: "logout"
end

Some Popular Web Frameworks

Ruby on Rails

Ruby on Rails, or just Rails, is an open source web development framework that uses Ruby as its programming language. Ruby is similar in syntax to Python, and it was chosen for Rails because of its elegant syntax and adaptability.

Rails uses ActiveRecord, an object-relational mapping library, to communicate with its database back-end. Rails also employs an MVC architecture.

Rails is great for web sites that follow a typical web design pattern: for example, blogs, e-commerce, and news sites.

Ruby on Rails Resources

Notable Sites using Ruby on Rails

Django

Django is an open source web development framework that uses Python as its programming language.

Django uses object-relational mapping for interacting with the database. It also uses an MVC architecture; note that Django calls the Controller the view and the View the template.

Because of Python's large following outside of the realm of web development, there are more general-use libraries available for Python than there are for a language like Ruby. As such, Django is great for sites that require complex server-side operations.

Django Resources

Notable Sites using Django

Other Web Frameworks

Bayong Rails, Django, and CakePHP, there are hundreds, if not thousands, of web frameworks. Here are some things to keep in mind when choosing a framework: