Hardware Reference
In-Depth Information
Fun with REST
If you've only done static HTML web
design before, you're probably used to the
idea that the slashes in a URL tell you in
what directory on a server the final HTML
file resides. However, it doesn't have to be
that way. You can tell the server how to
interpret the URL. In this project, you'll do
just that.
MATERIALS
ยป Account on a web server
It's most likely that your web server is running the Apache
web server program. If so, you can create a special file
inside any directory called .htaccess that determines the
behavior of the server with respect to that directory. You
can hide files and subdirectrories, password protect the
directory, and more.
Back in the "Network Identification" section of Chapter 9,
you wrote a PHP script that printed out all of the environ-
ment variables returned by the server when you make an
HTTP request. One of them was called REQUEST_URI . It
gave you the string of text that follows the GET request
from the client. That's the variable you want access to if
you plan to build a RESTful web service. Each of the pieces
of the request URI will be part of your interface, just like in
the example URLs shown previously.
For this project, you'll set up a base directory for the
project, and change the .htaccess file so that the server
redirects anything that looks like a subdirectory to the
script you're writing. Then you'll use the REQUEST_URI
variable to make your own RESTful interface.
NOTE: URI stands for Uniform Resource Indicator. It's a synonym
for Uniform Resource Locator, or URL, the term commonly used
for web addresses.
Change the Access
Start by
making
a new directory on your web server. Call
it myservice. Inside it, make a new file
called .htaccess. Files that begin with a
dot are invisible files on the Linux and
Mac OS X operating systems. So it's
best to make this file directly on the
server, either through the command
line or your favorite web-editing tool.
Here's the text of the file.
RewriteEngine On
# put in the base path to your directory:
RewriteBase /~username/myservice
# redirect anything after the base directory to index.php:
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
In order for this to work, your web host needs to
be running Apache's MOD_REWRITE module on its
server. It's a pretty common module, so it's likely
that it is, but if you find that this script doesn't work as
described, check with your system administrator to see
whether MOD_REWRITE is enabled.
!
This file tells the server to rewrite client
HTTP requests starting with the name
of this directory on your account. It
takes any string after the directory
name and replaces it with the index file,
index.php.
 
Search WWH ::




Custom Search