Chapter 10

USING SERVER-SIDE TECHNOLOGIES


It's pretty hard to find a website these days that doesn't use some kind of server-side scripting and database. So many different options are available, from commercial products to open source products, that it can be really daunting to decide which direction you want to go. In the case of web servers, web scripting languages, and databases, the open source alternatives are just as good (and in some cases even better than) the commercial options.

This chapter isn't going to walk you through the syntax of each of the various languages, and it isn't an instruction manual about how to set up and configure your servers. It is a guide to some of the different options available, as well as an explanation of the differences between the various products.

Occasionally, you'll have your choice of server technologies, and other times you will be required to use what's already in place. A lot of web scripting languages are reasonably easy to pick up for technically minded individuals. The best thing you can do is get some background in software design and programming (any introduction to programming course will do) just so that you know the basics of if-then-else, functions, and loops.

The server side removes barriers

In the early days of the World Wide Web, there was the saying that "content is king." Although that's still true today, visitors expect more from the Web than just a passive experience. Putting up a static page with some interesting text and a pretty picture just isn't enough anymore; end users expect interaction. News websites are a prime example of this evolution: readers are encouraged to discuss topics inline with the stories. Client-side scripting (JavaScript) just isn't enough anymore. To store input and interact with diverse data sources, websites have turned to server-side technologies.

JavaScript is limited in what it can accomplish, largely because of security concerns. Would you feel safe running an application on the Web that had free reign of your hard drive? How would you like a website that's capable of reading and writing any file on your hard drive? Some people think that JavaScript already has too much power, so the option exists within browsers to disable it. As a developer, you need to have confidence that what you've built will be able to run for everyone.

Web servers: dishing out hypertext

The web server along with the web browsers are the bare essentials of the World Wide Web. Web servers are computers that are constantly (we hope!) connected to the Internet, and they host websites. They respond to HTTP requests from browsers, serving a whole host of different types of files that reside online. Web servers are the front line in any website, handling requests by either filling them or passing them off to a web application server to dish up dynamic content.

As with everything in this chapter, you have a whole host of options available in web servers, and the best part is that most of them are free. Some servers are designed for specific purposes: lighttpd is a speed demon that is great at serving static content. Mongrel was developed specifically for serving Ruby on Rails applications. Others, like Apache, are generalists that can be easily extended through a series of modules.

Apache HTTP server

Apache is the reigning champion online and has been since 1996. It is an extremely popular web server that runs on virtually every operating system imaginable. (You can even run Apache on an iPhone!) There are a number of reasons for its success, but probably paramount is that it's reasonably fast, secure, and extensible.

Apache supports a module architecture where functionality can be added by loading new modules when the server starts. Because of this functionality, things like PHP are easy to add; instead of having to run a separate application server, PHP can be parsed directly through Apache, which makes things faster and easier to administer.

Microsoft Internet Information Services (IIS)

If Apache is the reigning champion, then Microsoft IIS is the first runner-up. Microsoft introduced its web server running on the Windows operating system in the mid-1990s. It gained popularity primarily because it was easy to configure and set up in comparison to Apache's command line and text file configuration mechanism (for some strange reason, people like their graphical user interfaces). IIS got quite a bad reputation in the beginning because early versions were full of security holes, opening up the entire machine to an industrious hacker and even launching self-propagating worms, which could then attack other IIS servers. One of the most notorious, Code Red, at its peak infected an estimated 360,000 IIS web servers and caused a noticeable degradation in network speed worldwide.

IIS has been growing in popularity as it's matured into a more secure and stable hosting environment. Today, it's estimated that IIS serves just less than 50 percent of the websites online today. Microsoft has bundled in its scripting language, Active Server Pages (ASP), into IIS, making it really easy to develop dynamic websites on the Windows platform.

lighttpd

A few years ago, Apache and IIS would be the only two web servers worth mentioning in terms of market share, but lately there have been some new servers that have seen increased use. One of the big players is lighttpd (Figure 10-1), which is a super-streamlined web server used for serving really large files. It doesn't have the flexibility of Apache or IIS, but it has discovered a niche in that it can serve data at much higher speeds.

image

Figure 10-1. lighttpd is gaining a foothold in Web 2.0 as larger, more flexible servers are traded in for smaller, faster, single-purpose servers.

Websites such as YouTube use lighttpd to serve their movie files, and some of the largest BitTorrent tracker websites (including the infamous The Pirate Bay) use lighttpd for serving up their static content (such as .torrent files).

Web servers rarely ever do the heavy lifting on a dynamic website or web application. That job is usually handled by a web application server, which handles the process of talking to databases and constructing content on the fly.

Mongrel

A little later in this chapter, you'll read about frameworks and why they're taking off in popularity. Leading the framework pack is Ruby on Rails, and Mongrel is a web server developed specifically for dishing out Rails applications. Mongrel is the uncontested leader in terms of speed, scalability, and stability in serving Rails applications, because it supports Ruby natively; other web servers deal with it via a module that executes Ruby code as a common gateway interface (CGI) script.

A wide range of hosting options

We're at a really great time for web development right now. The cost to host a website is falling, and it's easier than ever to outsource your hosting needs to a third party. If you're working for a big organization, chances are that they'll be running their own servers for web hosting. Smaller organizations and individuals will often opt for a hosting company, though. Running a server takes a lot of skill and resources: it has to be connected to the Internet with a fast connection (that DSL line you have at home just won't cut it), it should be backed up regularly, and it should be kept in a secure place, especially if you're storing customers' personal or financial information in a database. Not to mention, how many people have a diesel backup generator in case of power failure?

You should look at a number of different options when you're picking a company to host your website. Obviously, the features/application support will be a big one; if you've written a Ruby on Rails application, you'll need a server that supports Rails. Other considerations to take into account are bandwidth, disk space, machine specs, technical support, and an uptime guarantee.

Most hosting plans will give you a set amount of bandwidth every month. This is usually measured in the gigabytes of data transferred, but larger-capacity hosting plans may offer terabytes of bandwidth (1,000 gigabytes). You'll see a few different options in terms of types of plans offered, ranging from shared hosting to dedicated hosts. A shared host is exactly that: you're sharing the machine that hosts your website with other customers. If one of those other customers has a particularly high-traffic website or does something that locks up the server, your website will also be affected. Although that's not the end of the world for your personal weblog, a company that conducts business online may have a lower tolerance for frequent or extended outages.

The next step up from a shared host is a virtual machine. Virtualization software is special software that will let you run multiple operating systems, or multiple copies of the same operating system, off a single machine. If you're on a Mac and have ever used VMware Fusion or Parallels to run Windows, you're using virtualization software. The big advantage to a virtual machine over shared hosting is that each hosting account is housed within its own container. So although all customers on a single machine are sharing that machine's resources, each container is isolated from each other. If customer X does something that locks up their virtual machine, none of the other virtual machines on that computer will be affected. The added benefit from this is that because customers are isolated from one another, hosting companies are often willing to let customers have more control over their own account, such as install software and reconfigure system and server settings to optimize performance.

The final option is dedicated hosting where you get your own machine, hosted by some other company. This has all the advantages of virtual machine hosting, plus you don't have to worry about sharing resources (RAM, CPU, bandwidth) with other customers. Shared hosting is by far the cheapest option, whereas dedicated hosting will cost you a few hundred dollars a month (or more).

Picking the level of plan isn't that hard. Start with the least that you think you can get away with and then upgrade as you go. Most hosting companies are more than happy to let you upgrade your plan at any time; it means they get to charge a higher monthly premium! There's no point buying dedicated hosting for that application you're developing if you're not sure whether you're ever going to have more than a dozen users.

Picking a host can be really difficult, though. If you just look at ratings and reviews online, you'll never be able to find one. Every host has glowingly positive reviews and absolute horror stories posted about them. Your best bet is to ask around to friends, co-workers, and other professionals to see whether they have any recommendations for you.

Databases for web geeks

We recently posted a question on a blog asking people how they would describe a database to somebody who has absolutely no knowledge of computers. The common consensus was that people would use the analogy of a file cabinet, with database tables being file folders and the individual papers within those folders being records. It's not a bad analogy until you try to explain all the other parts associated with a database setup—things such as primary keys, foreign keys, relationships, and, of course, Structured Query Language (SQL).

Terminology

The field of database management systems is rife with all kinds of technical terms and acronyms. Here's a quick guide to some of the most common terms:

  • A database is a collection of data structured into records within tables. It might help to think of the difference between a relational database management system (RDBMS) and a database in these terms: the RDBMS is similar to a web server, whereas a database is similar to all the pages available on that server (except in a far more structured way).
  • A field, sometimes also referred to as a column, is a single piece of information contained within a record. Mary's phone number would be stored in a field in a database. All records within a single table will have identical fields.
  • A foreign key is the special piece of information that relates a record in one table to a record in another table. The foreign key in one table will refer to the primary key in another table.
  • A primary key is a special field within a table that uniquely identifies a record. In its most basic form, this could be a simple integer that increments by one for every record added. The reason tables have primary keys is so that each record can be uniquely addressed if changes need to be made to it. For example, I might have a table that contains a log of people buying things on a gift card. This log might list that "Ron" bought "1 cup coffee" every day for a month. If that's all it lists and (for some reason) you have to go in and say that he didn't, in fact, buy every seventh cup of coffee, you would have no way of addressing those particular records.
  • A record, sometimes also referred to as a row, is a grouping of similar pieces of data. So for example, a set of contact information for your friend Mary would be a single record.
  • A relational database is a database system in which information stored in different tables can be related to each other. You might use this if, for example, you were storing business contacts. Chances are that you know a few different people at a single company; instead of storing that company name 3, 10, or 50 different times in the record for each person, you could create a companies table and then just relate each person to the appropriate row in the companies table. The big advantage here is if the company gets bought out and changes its name from Acme Corp. to Wonderwhizz Inc., you need to change that name in a single location only.
  • Relational database management systems are applications such as MySQL and Oracle, which are commonly (and incorrectly) referred to as databases. These applications are actually RDBMSs, the software that interacts with a database.
  • A schema is the structure of the database, including the tables, fields, and any relations between tables.
  • Structured Query Language (SQL) is a standardized language developed specifically for interacting with relational databases. SQL lets you add, remove, update, and query data. There are small variations in SQL syntax from one database system to the next; but for the most part, once you have the basics of SQL mastered, you'll be able to work with practically any database system out there.
  • A table is a collection of records within a database. For example, you might have a table for "contact information" in which you list the addresses and phone numbers of a number of different people.

The world outside relational databases

A lot of the terminology covered previously is specific to relational databases. Most DBMSs available today are relational, because it's frequently advantageous to design your database schema in a relational manner in order to avoid duplication of information. But RDBMSs aren't the only game in town. There are a couple of other types of databases that you may run into online, so it's worth mentioning them here.

Object databases

Object databases aren't too common outside of very specific scientific applications. There is a very common and popular content management framework called Zope that uses an object database, however. The Zope Object Database (ZODB) is ideal in its application because it supports versioning natively, allowing someone to undo a change made on the website.

XML databases

XML is widely used online for data exchange between websites/web applications, but it's also an effective way of storing data. Occasionally this term will be used for referring to XML data stored within an RDBMS, but there are native XML databases that exist.

The biggest advantage to storing data in XML format is that XML is widely used for data exchange. Because of this, there doesn't need to be any conversion of data to/from XML, which saves on processing "costs" (that is, the server doesn't have a bunch of extra operations to perform).

XML databases store data in nodes, which is the correct term for data arranged within a hierarchical structure of XML tags. Instead of using SQL to query an XML database, two forms of querying syntax are widely in use: XPath and XQuery (which is just an extension of XPath). Each provides a way to extract data from an XML database by providing a syntax for addressing each of the databases' nodes.

Relational databases

A relational database simply means that data in one table can be joined (or related) to data in another table through the use of keys. Relational databases strive to normalize data, that is, to eliminate any duplication of data. Normalized data is a good thing: if there is duplication of data within a database, the chance exists for there to be a data anomaly, that is, when the two pieces of data become out of sync.

For example, if you were developing an online store with a series of products, chances are you would have a table listing each of those products along with their price and description. Occasionally, you want to be able to offer certain items on sale by marking them down 15 percent. Instead of creating a table of sale items and duplicating the product names, descriptions, and prices there, you would simply relate to the original products table. That way, if a product's price changes, it needs to be updated in only one place.

Structured Query Language (SQL)

There are four main types of interactions you'll have with databases using SQL: SELECTs for getting data out of a database, INSERTs for adding data to a database, UPDATEs for changing data in a database, and DELETEs for removing data.

Getting data out

What good is creating a database of information if you can never get anything out of it? If you return to the original file cabinet analogy, being able to find specific data within a database is the single biggest advantage over the old paper-based alternative.

How often have you looked at a big stack of papers and thought to yourself "Gee, I wish I could just run Google on that"? (OK, so it might just be us.) If you boil it down, though, search engines are really just great big databases full of information about the information on the Web. Every time you run a search on Google, you're running a query on a database.

In SQL terms, that query looks something like this:

SELECT url
FROM websites
WHERE content = "that thing I'm looking for"

If you dissect that statement, it's actually pretty easy to read. You're asking for the URL field from the websites table. Instead of just returning all the URLs stored in the table, you're narrowing the search down and getting only the URLs where the content field has that thing I'm looking for as the value.

There are a bunch of other options we could throw in here, such as an ORDER BY to sort the results or an INNER JOIN in order to relate the data from this table to the data stored in another table.

Putting data in

Your database would get stale pretty quickly if you couldn't add new information as it became available. SQL uses an INSERT to do this:

INSERT INTO websites (url, content)
VALUES ("http://www.amazon.com", image
"A really big store that sells a bunch of stuff")

Again, breaking this down, you're adding a record to the websites table. We've specified two fields in this table, url and content, and given them both values. Depending on how the table is set up, there may be other fields in the table that you haven't specified, but when you're creating a table, you have to go out of your way to specify that a field can have a NULL value; otherwise, running this SQL will give you an error.

Changing data

Shoot, that last addition we made makes it sound like Amazon is actually a store, located somewhere. We had better change the content field to clear that up:

UPDATE websites SET
content = "A e-commerce website that sells a wide range of products"
WHERE url = "http://www.amazon.com"

The first part of this SQL statement should speak for itself at this point. You're updating the websites table and setting the content field to have a better description. The last part (after WHERE) is the interesting part. You need to have some way to tell the RDBMS which record you want to update. In this case, you've done that with the URL field, but this could backfire on you. If there were more than one record with http://www.amazon.com listed as the URL, all those records would be updated as well. The flip side is that you may have specified that the URL, in fact, is the primary key, in which case there could not be multiple Amazon.com records listed. You would get an error if you tried to insert another one.

Removing data

Websites come and go. Although it's not likely that you'll be removing Amazon from your database for that reason, you have noticed that your favorite store for buying lawn gnomes is now offline. You had best keep things up-to-date:

DELETE FROM websites
WHERE url = "http://www.ultimatelawngnomes.com"

It's a nice, simple statement to remove ultimatelawngnomes.com from the database. The same caution applies with deleting records as does with updating records. You should try, whenever possible, to specify the primary key in the WHERE statement so as to limit your deletes to the actual record you intend to delete. In this example, it would be OK, because even if you had multiple records for ultimatelawngnomes.com and the site really had gone offline, you would probably want to eliminate all records. On the other hand, going into the HR database of a large company and running a DELETE on all records where the last name of the person is Smith may not be the best idea (that's why a lot of companies have employee ID numbers).

Your best bet for learning more about SQL is to check out http://www.w3schools.com/sql/. The site has a pretty good set of introductory lessons to SQL. If you get further into databases, be sure to check out the documentation specific to your RDBMS because it might have some specific functions that can save you time and significantly speed up your website.

A look at the RDBMS players

There are literally hundreds of different database systems, all great at different things. The ones listed in the following sections are the most common, so you're likely to run into them at some point when developing a website. We have limited the field here to database servers, so applications such as Microsoft Access didn't make the cut; although you could use Microsoft Access as the back-end for a website (we have before), it doesn't scale really well because it doesn't handle concurrent users all that efficiently. If you're comfortable with Microsoft products, you're better off upgrading to Microsoft Access's big brother—Microsoft SQL Server.

Oracle

The longtime heavyweight in the database world and reigning champ is definitely Oracle. Chances are really good that if you have a large organization running a database, that database will be Oracle. Having been around since the late 1970s, Oracle is a tested and proven solution for storing and retrieving large quantities of data (it's also the most expensive option available). Oracle is well known for its ability to handle extremely large data sets and to work with them in a fairly efficient manner (meaning running a query on a million records isn't a daylong activity).

Microsoft SQL Server (MSSQL)

Microsoft introduced a commercial RDBMS to try to compete with Oracle. Early on, it just wasn't up to snuff at handling large amounts of data, and it experienced a few security vulnerabilities. It's fair to say that because of its rocky start, Microsoft hasn't really made a dent in the market that Oracle addresses (really big databases/applications), but it's still a pretty big player in medium-sized applications. Those customers who need an Oracle-sized system will still buy Oracle. Those who think that would be overkill will generally either go with Microsoft or go with one of the open source alternatives.

MySQL

MySQL is the most popular open source database system available. It gained a great deal of popularity early on because it was really fast compared to PostgreSQL and it worked amazingly well for web applications. A number of large Internet companies have skipped the Oracle bandwagon altogether and have invested heavily in MySQL as their primary RDBMS.

Even though the software is open source, there is still a commercial company behind MySQL so that if a company wants to use it, they'll have someone to lean on for support. MySQL AB (the company behind MySQL) was recently purchased by Sun Microsystems, a hardware and software company that has been around for quite some time.

One of the big reasons for MySQL's popularity amongst open source RDBMSs is that a number of excellent front-ends are available. phpMyAdmin is an interface written entirely in PHP, and it's hosted online, which allows developers to create, delete, and modify databases and tables within MySQL. MySQL AB has released a number of GUI tools for Windows, Linux, and Mac OS X that eases development and server administration. Figure 10-2 shows an excellent commercially available front-end editor, Navicat.

Figure 10-2. Navicat is another popular, commercially available front-end for MySQL that enables developers to quickly build and modify databases.

PostgreSQL

PostgreSQL is another excellent open source RDBMS that is completely driven and maintained by the user community. A couple of companies provide commercial support for PostgreSQL, but they are only peripherally involved in the continued development and evolution of the software (they're members of the user community, just like everybody else).

PostgreSQL's biggest selling point is that it's quite Oracle-like in the way it works and in its syntax subtleties. Therefore, companies that are looking for open source alternatives to try to rid themselves of the burden of high Oracle licensing fees will often look to PostgreSQL because it's the path of least resistance.

Other data sources

We talked a lot in the previous chapter about interoperability and sharing data between different websites and web applications. This is generally accomplished using XML, but there's a couple of other terms and acronyms that you should probably be aware of when discussing data exchange:

  • Web services are software systems that allow for computer-to-computer interaction over the Web (not necessarily involving the interaction of a person). It's a general term that can be used to refer to something as complex as an API or something as simple as an RSS feed from a weblog.
  • SOAP is a protocol for exchanging XML messages over the Web (using HTTP or HTTPS).
  • A remote procedure call (RPC) is a way of having one computer initiate a procedure on another computer without having the details of this interaction explicitly programmed. So, in other words, RPC is a way of getting another computer on the network to do something and return a result to your computer. Most commonly on the World Wide Web, RPC will be used in conjunction with XML for data exchange between machines (XML-RPC).
  • An application programming interface (API) is a series of "hooks" built in to an application that allows external access to the application's data and functionality.

One of the best things about developing applications for the Web is that there is already a wide array of data repositories available to tie in to your application. It's easy to integrate input from your users with map data available from Google and housing information stored in a database, as some realtors are now doing.

Web application languages

Similar to this look at databases, you'll find a world of options in web application languages. These are commonly called scripting languages, or server-side scripting languages to be more precise. Each of the options uses a different syntax, but at the end of the day, they all pretty much do the same thing. Some may have certain strengths over others, but when it comes to small to medium-sized web applications, you really won't notice a huge difference in performance between any of the players.

If you're just starting out and you're not sure what language is best to pick up, we recommend sticking to PHP. It's free and is widely used, and PHP hosting plans are offered for next to nothing from a wide range of companies. Whatever you pick, don't sweat your decision too much. We have one friend who's a brilliant Python programmer and complains a great deal about the lack of job postings for Python developers. The fact of the matter is that knowing Python, he can easily pick up any number of other languages and be proficient in a matter of weeks (and he has for various projects).

PHP

By far the most popular scripting language on the Web amongst developers is PHP, which is free and is often installed with the Apache web server (the most common web server on the Web). PHP was designed to be a scripting language for the Web, so it focuses a great deal on the needs of web developers.

Chances are that if you've downloaded an open source web application (such as WordPress, Drupal, or PHPBB), the tool is written in PHP. PHP is widely used amongst open source web application developers, because it is often self-contained; end users don't have to install a bunch of other software in order to get things working. To install a PHP application, it's usually as easy as uploading the files to your web host, creating a database, and editing a configuration file (of course, depending on the application, your mileage may vary). Organizations of all sizes have rolled out PHP-based applications in some capacity (Figure 10-3).

image

Figure 10-3. Drupal is an excellent open source content management system built with PHP. Even big companies such as SonyBMG are starting to see the benefits of using free software for their online applications.

PHP is a great starting point for people wanting to learn web application development. Because it's free, anyone can download a copy and install it on their own web server (if you don't feel like spending a few bucks for a hosting plan). PHP got a bit of a reputation for having security problems early on, which has prevented its adoption in a lot of large organizations, but that's definitely starting to change. Organizations we know that had previously never touched PHP are rolling out redesigns of their corporate websites using PHP-based content management systems.

Ruby

Ruby is a computer language that has been around since 1993, but it previously was not considered a strong contender for developing web applications. It's only with the introduction of the Ruby on Rails framework (discussed in a moment) that Ruby has joined the mainstream for web application development.

The jury is still out on whether Ruby is ready for the mainstream Web. Personally, we think that its use in some very large-scale applications (Basecamp, Twitter, Yellowpages.com) speaks for itself. Ruby, when used in Ruby on Rails, is the fastest growing web-programming language of the past three years. Developers are tired of having to repeatedly develop the same "type" of application and have turned to Ruby on Rails to streamline the process.

You'll probably see Ruby on Rails slowly sneak into big companies as developers try it and develop internal applications with it. We can't see Rails' use decreasing any time soon, and it's more than likely to continue to steal market share as companies search for faster, more agile ways to develop web applications.

Python

Python, similar to Ruby, has been in existence since the early 1990s, but it's not until recently that it has been adopted as a web application language in the mainstream. Zope, which is an excellent open source web application server intended for content management purposes, is the largest user of Python.

Python is a little bit of an odd-man-out in web application development. It's a great, fully featured language, but for whatever reason, it's not really in the mainstream. It has gotten a little traction lately amongst small web startups because it doesn't have the bad reputation brought about by PHP's security issues early on.

ASP (.NET)

Active Server Pages is the Microsoft-centric solution to web application development. The biggest advantage to ASP is that it integrates well with other Microsoft offerings; users can be authenticated off Active Directory servers easily, for example. ASP has been around for quite some time and is available not only on Microsoft IIS servers. Sun Microsystems makes a server product that allows ASP to run on a variety of different operating systems including its own Solaris and Linux.

Java/JSP

Java/JavaServer Pages (JSP) were quite popular during the first web boom in the 1990s and have somewhat died off since. Java was a quite common skill for developers during the dot-com boom but is pretty onerous to use for developing web applications. JSP didn't die out because it lacked functionality or because it was inferior technology, but rather because development cycles took too long. We don't mean for it to sound like we're ringing the death bell for JSP—it's still used in a number of enterprises, but you don't see many startups choosing JSP for their applications these days.

ColdFusion

ColdFusion is a commercial web application server produced by Adobe. It has a syntax that is similar to HTML, and it uses CFML tags and attributes to specify functionality. It's because of this that we've found ColdFusion to be extremely easy to learn and an excellent starting point for people wanting to learn web application development. There is excellent documentation and books available to get you up to speed quickly.

ColdFusion has, historically, been widely used in commercial websites but has seen a decline in recent years as open source scripting languages have increased in quality and popularity. It's hard to justify spending $1,000 for a web application server when you can have the same functionality for free using one of the many alternatives. ColdFusion is used heavily still in intranet development because it has built-in support for a wide array of database management systems as well as products such as Microsoft Exchange (which is used for e-mail/calendaring in a number of large corporations).

Frameworks

A framework is one step above a web application language. Frameworks are a collection of functions and libraries that make web development easier by automating some of the common (and tedious) tasks in developing a website. Things such as user login/logout and access control are used in a lot of different web applications, so why would you want to write code to manage that function over and over again? Similarly, a lot of web applications use a database on the back-end to do adds, updates, deletes, and listings; wouldn't it be nice if there were a simple way to build a basic page to add a record to the database, without having to rewrite SQL repeatedly?

Why bother with frameworks?

Frameworks aren't a new idea, but they really seem to have taken off in popularity recently. Everyone seems to have an idea about how best to implement a framework: some people prefer flexibility to efficiency; others just want to keep things simple. Most of the major frameworks all support similar features; they just take a different approach to how they do it. Most frameworks are language-specific; however, there are a few that have been ported between languages.

For the most part, you need to learn the language before you become proficient with a framework. Once you do reach that point, however, frameworks can save you a great deal of time and really allow you to focus on the big picture instead of worrying about writing code to interact with your database (as one example). There is no right or wrong time to use a framework; if you are comfortable working in a particular language, it might be a good time to branch out and explore the frameworks available to you. Big project or small project, they will save you time.

A few popular candidates

Frameworks are available for every language and every purpose. We've limited the discussion to three here, all based on popular open source languages. If you're not into Ruby, PHP, or Python, however, just run a search for <language> framework, and we guarantee you'll get at least two or three solid results.

Ruby on Rails

Ruby on Rails (RoR) is a great example of a web development framework. An enterprising developer, David Heinemeier Hansson, created Ruby on Rails when he was working on a project management web application. He grew bored of always having to rewrite the same code over and over again and realized that some of the process could be automated.

Frameworks can be great boosts to productivity but can also impose constraints upon your development process. For example, Ruby on Rails applies a "convention over configuration" philosophy to development: if you follow certain conventions, the code will write itself; otherwise, you'll end up rewriting a bunch of stuff by hand. One example of this is if you create a class called product (meant to add/delete/edit products in your database), Ruby on Rails will assume that the table in your database storing product information will be called products. It's not a big deal, but some organizations may have a strict naming scheme in place for database tables, such as you have to put the name of the department before your table name. In cases like that, you're going to have to do a little extra legwork (you don't have to write off Rails; you'll just need to spend a little extra time configuring things instead of using some of the built-in functionality).

Another feature of Rails (and several other frameworks for that matter) that's worth mentioning is its extensive implementation of the Model-View-Controller (MVC) architecture (Figure 10-4). We talked about separating content from form from function using XHTML, CSS, and JavaScript. MVC is sort of like an implementation of this in server-side development.

image

Figure 10-4. MVC request/response loop. 1) The browser initiates a request to the controller, and 2) the controller interacts with the model, which 3) may or may not interact with a database. 4) The model returns a result to the controller, which 5) invokes a view that is 6) returned to the browser.

The model handles all the data and logic in an application. It isn't directly responsible for data storage. Although we've focused on databases in this chapter, there are certainly other ways of storing data, such as a flat text file or as an XML file. The model is more interested in "working data," such as the total for this person's cart is $99.95 or that Friday is three days from today.

The view is the presentation layer. Any sort of user interface rendering is handled through the view, from the colors used on a page to the placement of check boxes and input fields in a form. The view will often generate the UI on the fly based on information from the model.

The controller is responsible for handling input, interacting with the model, and returning output back to the view. All of the application legwork is done in the controller.

CakePHP

If Ruby on Rails isn't your thing or if you're already a whiz at PHP but you love the idea of what a framework has to offer, you're in luck. There is a pretty long list of frameworks available for PHP, and most of them implement MVC architecture. CakePHP is one such framework for PHP that has grown in popularity since its invention in 2005. Although it shares a number of features of Ruby on Rails, it's been written from the ground up as its own distinct and powerful framework.

One of the big advantages of CakePHP is its ease of use and excellent documentation. If you're new to PHP, you may just want to skip all the groundwork and dive right into CakePHP—chances are you'll be able to accomplish 95 percent of what you set out to do with it right out of the box.

Django

The Python developers out there need not feel left out either—or the ColdFusion developers, ASP developers, or pretty much anyone else; there is a framework for any web application language. Django offers similar features to the other frameworks we've covered and has been used on a few high-profile Web 2.0 web applications (such as Revver and Pownce).

Of special note, Google recently announced its Google App Engine application platform (Figure 10-5), where you can essentially host your web application with Google. Google App Engine has built-in Python support, and developers appear to be using Django quite extensively in the early applications that have been built. It's definitely a great boost for the Python programming language and the Django framework.

image

Figure 10-5. Google has announced App Engine, an application environment with built-in support for Python developers. Now, you can build an application that runs off Google's infrastructure and has the same scalability.

Summary

As we mentioned in this chapter, it doesn't really matter what application server, language, or framework you choose (if you even have the choice). They're all mature enough products that they are equally capable in their feature sets. If you're new to the area, pick something open source because they're the easiest and least expensive to learn on.

It's easy to get bogged down in language/database/framework discussions on projects. If you're going to be doing the technical development, you should ultimately be the one making the decision on what to use. Developers have to be confident in the tools they use; if the back-end architecture is being dictated to you but it's something you've never worked with, be sure to either get some help or allow extra time for learning. End users can tell whether there are problems in a site's construction.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.143.41