"Clean URLs" also known as "RESTful URLs" are user-friendly, purely structural, and do not contain a query string. Instead they contain only the path of the resource.
ie: "http://twitter.com/users/show/"+username+".json"
Questions about server-side functionality:
Do I need to make a unique server-side API script for each directory?
Can I forward all requests to just one script if so how do I pull useful information from the Clean URL structure ($_GET['url_structure'])?
Why does twitter call to a .json file that surely doesn't exist. It must get generated on request. How does this work? This leads me to believe that the answer to question 2 is yes.
1) Not if you use a RESTful framework like RecessPHP or if you use a mod_rewrite rule in your .htaccess file to redirect all API requests to a single PHP file (known as the front controller).
.htaccess
api.php
2) You can use the rewrite module of apache to redirect all api requests to a special PHP file that handles them. Depending on your apache configuration, the original requested (RESTful) url will be stored in a server variable in PHP, I believe it's
$_SERVER['REQUEST_URI']
. Of course you could also just pass along a$_GET[]
variable to PHP that contained the RESTful url..htaccess
api.php
3) Twitter (and many other APIs) use this because it is a convenient way of supplying the format that an application expects back from an API. All of the API requests are rerouted to a single PHP file which handles creating all the files and echoing their contents to the output. The file is never actually stored on the server (unless it is cached).
Good Resources
A note on RecessPHP. It's certainly a great tool and I would encourage you look at it (maybe at its source to get an idea of how it processes things), but that said, it seems a bit clunky to me. The fact that path names are written in special comments seems very not-PHP to me. I'd stray away from this, and I wouldn't call it the perfect framework, but it's certainly a start. Good luck!
This worked for me: Put this in the htaccess file at the root of your website.
And then if you go to the page http://localhost/api/person/susan You will see that it takes you to the file at http://localhost/api/api.php I also have a recipe page that I go to using http://localhost/recipe/edit/2 Put this in the api.php file:
The variables above will hold the category: recipe, action: add or edit, and the data which can be a number which is the id for the recipe or what ever you want it to be. Then inside the add_recipe.php use the variables to determine whether you are editing or adding a recipe. And if you use the api you can include different files depending on what ajax request you are using to talk to your api.