Difference between revisions of "Web Frameworks"

From CSE330 Wiki
Jump to navigationJump to search
 
(14 intermediate revisions by 4 users not shown)
Line 1: Line 1:
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.
+
''The following recommendations were last updated Fall 2024''
  
Web Frameworks are an alternative method for writing web applicationsWeb Frameworks are designed to make the experience for the developer more elegant and increase the possibilities for collaboration.
+
So far in this class, you have been using PHP, HTML, and JS to run your websiteYou may have found that PHP can become burdensome for larger applications and difficult to use in collaboration.
  
There are ''hundreds'' of Web Frameworks out thereWe 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.
+
Web Frameworks are an alternative method for writing web applicationsFrameworks exist to make writing code more maintainable, and scalable, as well as to introduce base functionality for the application you make.
  
== Common Features of Web Frameworks ==
+
There are ''hundreds'' of frameworks available.  We've categorized and listed some of the more popular below to help you decide what's right for your application.
  
=== Less Code ===
+
== Common Features of Frameworks ==
 +
 
 +
=== Routing ===
 +
 
 +
A '''key difference''' between PHP and web frameworks is how routing occurs.
  
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!
+
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''.
  
=== Object-Relational Mapping ===
+
With routers, however, you can set arbitrary URLs to run different controller files.
  
Many Web Frameworks use an [[wikipedia:Object-Relational Mapping|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:
+
=== Less Code ===
  
<source lang="ruby">
+
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) website in a Web Framework without writing a single line of HTML or CSS!
u = User.find_by_username("alice")
 
u.update_attributes(
 
  nickname: "Ali"
 
)
 
</source>
 
  
 
=== MVC Architecture ===
 
=== 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.
 
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, [[wikipedia:Model–view–controller|read the article on Wikipedia]].
  
 
A directory structure for an MVC framework might look like the following:
 
A directory structure for an MVC framework might look like the following:
Line 42: Line 43:
 
# The final view is sent to the user.
 
# The final view is sent to the user.
  
=== Routing ===
+
== So what do I do? ==
  
A key difference between PHP and web frameworks is in how routing occurs.
+
=== Front-end Frameworks ===
 +
=====React=====
 +
Get started [https://react.dev/ Here]
  
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''.
+
React is the most popular front-end framework. React uses JS to create HTML (JSX), as opposed to updating HTML with JS. This makes manipulating the DOM more efficient and smoother.
  
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.
+
=====Vue=====
 +
Get started [https://vuejs.org/v2/guide/ Here]
  
Here is a sample router file from a Ruby on Rails web application so you can get an idea of how it works:
+
A really good happy medium between React and Angular. Not fully contained, but separates/organizes your code very nicely. Don't have to use JSX or Angular-specific methods. The code structure is better organized than React, but not as opinionated as in Angular.
  
<source lang="ruby">
+
Vue is partnered with PHP Laravel so a lot of the integrations are very code-friendly.
MovieNews::Application.routes.draw do
 
  resource :session, only: [:new, :create, :destroy]
 
  
  resources :users
+
=====Angular=====
 +
Get started [https://angular.io/guide/quickstart Here]
  
  resources :movies do
+
Not to be confused with Angular.js (the old version), Angular is the Google-developed front-end framework that competes with React and Vue. Angular has its own more opinionated code practices, making it easy for developers to understand but with a higher initial learning curve.
    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 ==
+
=== Back-end Frameworks ===
 +
=====Express (NodeJS)=====
 +
Get started [https://expressjs.com/en/starter/generator.html Here]
  
=== Ruby on Rails ===
+
If you're planning on using a NodeJS backend, Express is a great option. Other options include [https://hapijs.com/ Hapi.js] or [https://koajs.com/ KOA] if you're already familiar with Express and want to try something else.
  
Ruby on Rails, or just '''Rails''', is an open source web development framework that uses [[wikipedia:Ruby (programming language)|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.
+
=====Laravel (PHP)=====
 +
Get started [https://laravel.com/docs/5.8 Here]
  
Rails uses ActiveRecord, an object-relational mapping library, to communicate with its database back-end. Rails also employs an MVC architecture.
+
Laravel is certainly the most popular PHP back-end option. If you aren't sure you want to venture over to a Node, Python, or another back-end, I urge you to try Laravel instead of vanilla PHP. Super clean, super simple, and genuinely makes writing PHP enjoyable. As mentioned above, it's coupled extremely well with Vue.js, I would recommend searching for tutorials of the two together if that's the approach you'd like to take.
  
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.
+
=====Django (Python)=====
 +
Get started [https://docs.djangoproject.com/en/2.1/ Here]
  
Rails is great for web sites that follow a typical web design pattern: for example, blogs, e-commerce, and news sites.
+
Django's tagline is "The Web framework for perfectionists with deadlines". Take that as you will.
  
==== Ruby on Rails Resources ====
+
Django is a fairly opinionated full-stack framework, meaning both your front-end and back-end will be written and handled by Django (as opposed to, say Express, which can integrate with whatever sort of front-end you'd like). This doesn't mean it's more difficult to learn, but it does kind of force you to learn how Django does its front-end and back-end which is why it counts for both in terms of points.
  
* [http://rubyonrails.org/ Official Web Site]
+
=====Flask (Python)=====
* [http://guides.rubyonrails.org/ Guides and Tutorials]
+
Get started [http://flask.pocoo.org/ Here]
* [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 ====
+
Flask is a micro back-end framework and is extremely easy to setup. Unlike Django, Flask won't handle your Front-end for you. This leaves you the option to use any of the recommended front-end frameworks or to just use regular HTML/JS.
  
* [http://twitter.com/ Twitter]
+
=====Bottle (Python)=====
* [https://github.com/ Github]
+
Get started [https://bottlepy.org/docs/dev/ Here]
* [http://www.groupon.com/ Groupon]
 
* [http://www.yellowpages.com/ YellowPages]
 
  
=== Django ===
+
Same gist as Flask, just an alternative.
  
'''Django''' is an open source web development framework that uses Python as its programming language.
+
=== Other and Database Info ===
 +
=====MongoDB=====
 +
Get started [https://docs.mongodb.com/manual/ Here]
  
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''.
+
MongoDB is a very popular no-sql based database. It may seem like a hassle to learn another type of database querying, but as soon as you realize you don't have to mess with foreign keys and it's all essentially javascript objects, it becomes a pretty attractive option. They also have free cloud databases now which is awesome because you don't have to worry about hosting a database on your computer, but much of the security is handled on their end.
  
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.
+
=====Firebase=====
 +
Get started [https://firebase.google.com/ Here]
  
==== Django Resources ====
+
Firebase is another cloud-based no-sql option. Firebase provides everything from the database itself to real-time crash reporting and analytics. Very easy to integrate into your app and get started with.
  
* [https://www.djangoproject.com/ Official Web Site]
+
=====Other SQL=====
* [https://docs.djangoproject.com/en/1.4/ Django Documentation]
+
[https://www.postgresql.org/ PostgreSQL]
  
==== Notable Sites using Django ====
+
[https://mariadb.org/ MariaDB]
  
* [http://www.washingtontimes.com/ The Washington Times]
+
=== Structuring Your Project ===
* [http://www.mozilla.org/ Mozilla]
+
Once you've outlined your project and have chosen what languages and frameworks you'd like to use, knowing where to start can be a bit daunting. We recommend a straightforward file structure similar to the following:
* [http://www.pbs.org/ PBS]
 
* [http://instagram.com/ Instagram]
 
  
=== CakePHP ===
+
<source lang="bash">
 
+
client/
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.
+
server/
 
+
config/
CakePHP uses object-relational mapping for database interaction, as well as an MVC architecture.
+
entrypoint
 
+
</source>
CahePHP is great for small or large web sites that can benefit from using a language that you already know.
 
 
 
==== CakePHP Resources ====
 
 
 
* [http://cakephp.org/ Official Web Site]
 
* [http://book.cakephp.org/2.0/en/getting-started.html Getting Started Guide]
 
* [http://api20.cakephp.org/ Documentation]
 
 
 
==== Notable Sites using CakePHP ====
 
 
 
* [http://www.wiidu.com/ Wiidu (a Craig's List in Africa)]
 
* [http://www.store-locator.com/ Store-Locator.com]
 
* [http://www.cdrecyclingcenter.com/ CD Recycling Center of America]
 
* [http://www.wipe.com.ar/ Wipe Buenos Aires]
 
  
== Other Web Frameworks ==
+
Depending on what frameworks you're using, it's easy to just place your full front-end framework in the client folder, all of your back-end files in the server folder, and all of your database and other configurations in the config folder. Your entry point is just a file containing the command(s) to start everything. Whether this is a package.json file, or a python file that starts your Django server, it's nice to keep it in the root of your project.
  
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:
+
Have fun!
  
*
+
[[Category:Module 5]]

Latest revision as of 20:39, 6 June 2024

The following recommendations were last updated Fall 2024

So far in this class, you have been using PHP, HTML, and JS to run your website. You may have found that PHP can become burdensome for larger applications and difficult to use in collaboration.

Web Frameworks are an alternative method for writing web applications. Frameworks exist to make writing code more maintainable, and scalable, as well as to introduce base functionality for the application you make.

There are hundreds of frameworks available. We've categorized and listed some of the more popular below to help you decide what's right for your application.

Common Features of Frameworks

Routing

A key difference between PHP and web frameworks is 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.

With routers, however, you can set arbitrary URLs to run different controller files.

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) website in a Web Framework without writing a single line of HTML or CSS!

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.

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:

  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.

So what do I do?

Front-end Frameworks

React

Get started Here

React is the most popular front-end framework. React uses JS to create HTML (JSX), as opposed to updating HTML with JS. This makes manipulating the DOM more efficient and smoother.

Vue

Get started Here

A really good happy medium between React and Angular. Not fully contained, but separates/organizes your code very nicely. Don't have to use JSX or Angular-specific methods. The code structure is better organized than React, but not as opinionated as in Angular.

Vue is partnered with PHP Laravel so a lot of the integrations are very code-friendly.

Angular

Get started Here

Not to be confused with Angular.js (the old version), Angular is the Google-developed front-end framework that competes with React and Vue. Angular has its own more opinionated code practices, making it easy for developers to understand but with a higher initial learning curve.

Back-end Frameworks

Express (NodeJS)

Get started Here

If you're planning on using a NodeJS backend, Express is a great option. Other options include Hapi.js or KOA if you're already familiar with Express and want to try something else.

Laravel (PHP)

Get started Here

Laravel is certainly the most popular PHP back-end option. If you aren't sure you want to venture over to a Node, Python, or another back-end, I urge you to try Laravel instead of vanilla PHP. Super clean, super simple, and genuinely makes writing PHP enjoyable. As mentioned above, it's coupled extremely well with Vue.js, I would recommend searching for tutorials of the two together if that's the approach you'd like to take.

Django (Python)

Get started Here

Django's tagline is "The Web framework for perfectionists with deadlines". Take that as you will.

Django is a fairly opinionated full-stack framework, meaning both your front-end and back-end will be written and handled by Django (as opposed to, say Express, which can integrate with whatever sort of front-end you'd like). This doesn't mean it's more difficult to learn, but it does kind of force you to learn how Django does its front-end and back-end which is why it counts for both in terms of points.

Flask (Python)

Get started Here

Flask is a micro back-end framework and is extremely easy to setup. Unlike Django, Flask won't handle your Front-end for you. This leaves you the option to use any of the recommended front-end frameworks or to just use regular HTML/JS.

Bottle (Python)

Get started Here

Same gist as Flask, just an alternative.

Other and Database Info

MongoDB

Get started Here

MongoDB is a very popular no-sql based database. It may seem like a hassle to learn another type of database querying, but as soon as you realize you don't have to mess with foreign keys and it's all essentially javascript objects, it becomes a pretty attractive option. They also have free cloud databases now which is awesome because you don't have to worry about hosting a database on your computer, but much of the security is handled on their end.

Firebase

Get started Here

Firebase is another cloud-based no-sql option. Firebase provides everything from the database itself to real-time crash reporting and analytics. Very easy to integrate into your app and get started with.

Other SQL

PostgreSQL

MariaDB

Structuring Your Project

Once you've outlined your project and have chosen what languages and frameworks you'd like to use, knowing where to start can be a bit daunting. We recommend a straightforward file structure similar to the following:

client/
server/
config/
entrypoint

Depending on what frameworks you're using, it's easy to just place your full front-end framework in the client folder, all of your back-end files in the server folder, and all of your database and other configurations in the config folder. Your entry point is just a file containing the command(s) to start everything. Whether this is a package.json file, or a python file that starts your Django server, it's nice to keep it in the root of your project.

Have fun!