Pretty URLs: Pretty Easy!
Back when I wrote the third edition of Build Your Own Database Driven Web Site Using PHP & MySQL, it was still acceptable (though certainly unfashionable) to lump all of the code together to generate a web page into a single file, and then use a URL naming that file to access the page.
PHP has moved on. These days, a developer that builds web sites that way is unlikely to gain a job (except perhaps one that pays poorly). Developers are expected to organize their code into multiple files, and URLs containing file names like index.php are definitely on their way out.
Experienced developers will use finely-crafted frameworks and web server configurations to convert so-called pretty URLs like http://www.example.com/thingy/. No ".php" advertising the fact that the site is built in PHP, no specific filename at all. URLs like this one are designed to be as simple and memorable as possible.
For the fourth edition of the book, I wanted developers to make a habit of this earlier, without forcing them to fiddle with their server configuration, or learn a complex PHP framework.
The easiest possible way I could find to produce pretty URLs was to take advantage of a feature built into every PHP-equipped web server out there: a URL that points to a directory on the server will automatically run a PHP script named index.php
sitting in that directory.
With that in mind, here’s how all of the examples in the book from Chapter 3 onward are structured:
-
A folder
Everything goes inside a folder, whose name is the only visible part in the URL. So if the example displays "thingies", you would start by creating a folder named thingies, and the URL of the example would be http://www.example.com/thingies/ -
A controller, index.php
When a browser requests the example’s URL, the server will automatically run the index.php script contained in the folder. This file is called the controller, and contains all the logic for handling browser requests and performing tasks in response. -
Include files, like db.inc.php
Pieces of code that will be useful in more than one example are pulled out of index.php and placed in separate PHP files. This is basic stuff for PHP veterans, but it’s a valuable coding technique that was covered much later in the third edition, in Chapter 12. Now it’s in Chapter 3. -
Template files, like thingy.html.php
Once the controller (with the assistance of one or more include files) does all the work, it must prepare a web page to send back to the browser in response. Rather than slopping all the HTML code into index.php (which is what we did all the way through the third edition of the book), the fourth edition uses a separate file for the HTML code for each type of page. These template files consist mostly of pure HTML; the only PHP code they contain is there to output the values of PHP variables created by the controller. The code in these files is kept very simple, so that they can be handed off to designers who may be unfamiliar with PHP at all. Also, using separate files makes it easy to generate several different types of page (that is, serveral different template files) from a single controller.
All the little changes in approach like this one are what really makes me proud of Build Your Own Database Driven Web Site Using PHP & MySQL, 4th Edition. I used to describe the third edition of this book as giving beginners "just enough rope to hang themselves with."
With the fourth edition, beginners can breeze past the self-strangulation stage — hopefully straight into a well-paying career as a PHP developer!
I bet you’re wondering where the code is, right? To see how the “pretty URL” technique described above looks in code, be sure to check out Chapter 3 when we publish it on Thursday. Or if you’re unable to wait, grab all four chapters in PDF format today — free of charge.