Difference between revisions of "Web Frameworks"
|Line 73:||Line 73:|
== Some Popular Web Frameworks ==
== Some Popular Web Frameworks ==
Revision as of 03:35, 8 April 2013
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.
- 1 Common Features of Web Frameworks
- 2 Some Popular Web Frameworks
- 3 Other Web Frameworks
Common Features of Web Frameworks
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!
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:
# Ruby on Rails ORM Example u = User.find_by_username("alice") u.update_attributes( nickname: "Ali" )
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.
You learned about the Model-View-Controller architecture in CSE 132. If you need a review, read the article on Wikipedia.
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:
- The server routes the request to a certain controller.
- The controller interprets the request, loading requested information from the models.
- The controller passes the information from the models to a view.
- The final view is sent to the user.
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:
# Ruby on Rails Routing Example 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
Important Word of Caution: This means that you should not save your framework source code in a directory that Apache serves! For example, if you put your Django site in /var/www/mydjango/, people could navigate via their web browser to example.com/mydjango/ and see all of your source code, including sensitive business logic and passwords! Do not make this mistake.
Some Popular Web Frameworks
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.
We will be using Django as the standard web framework for Module 5. We made this choice because of the fact that Django runs on Python.
Notable Sites using Django
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.
One feature of Rails that differentiates it from other frameworks is its concept of database migrations. Whenever you need to change your database schema, Rails lets you do so by generating a migration. In tue future, if the change wasn't what you wanted, your solution is easy: just rollback the migration.
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
If you want to stay in the familiar land of PHP, CakePHP is an open source web framework similar to Ruby on Rails and Django but written with PHP instead of Ruby or Python. Almost everything in CakePHP takes advantage of the object-oriented power of PHP5 to make development more agile.
CakePHP uses object-relational mapping for database interaction, as well as an MVC architecture.
CahePHP is great for small or large web sites that can benefit from using a language that you already know.
Notable Sites using CakePHP
Other Web Frameworks
Beyond 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:
- All good frameworks will take care of rudimentary security details for you, like preventing CSRF attacks and sealing up any XSS holes. Read your framework's security article before continuing. If your framework does not have a security article, you probably shouldn't be using that framework.
- Make sure that the framework is still being maintained. You don't want to start using a framework that hasn't been updated in several years.