0

According to what I've read about RESTful services, one is supposed to be able to get a "resource" (like a database record) using a request to a URL with the record id at the end, like this: localhost/my_records/1. Accessing such a URL would display a web page with the record information for id 1.

I need to implement something like this in PHP/Apache/MySQL but I am still relatively new to the language. The most I can manage to create is something like localhost/my_records/index.php?id=1.

Creating a ["/id"] url seems to require creating that folder. Is there any way to achieve this?

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
tutiplain
  • 1,427
  • 4
  • 19
  • 37
  • 2
    It sounds like you want to do URL rewriting. Look up mod_rewrite for apache. An example would be codeigniter which has the ability to rewrite this http://www.mysite.com?id=1 to http://www.mysite.com/id/1 and achieve the same results. – Chad May 07 '13 at 19:38
  • [Slim Framework](http://www.slimframework.com/) might be handy for you. – Alex Howansky May 07 '13 at 19:56

3 Answers3

1

Configure your webserver to route all requests through a single PHP script. This will allow you to parse the retrieved URL. Since you are using Apache mod_rewite is the way to go.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [L]

This will route all requests for paths/files that do not exist (remove lines 2 and 3 if you want anything routed to that script) through index.php. In the script you can then access the accessed URL via $_SERVER['REQUEST_URL']

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
1

You can have a RESTful service with any kind of URL, you don't need to do a URL with the id at the end, but this type of URL is better for convenience and good design principles.

Otherwise this type of URLs is also know as "friendly URL", most PHP frameworks provide this type of URL, for example, Symfony, Code Igniter, Laravel... provide this functionality.

If you want this type of URL without use any PHP framework, you can check this answer and responses: How to create friendly URL in php?

In my opinion, a good framework to implement a good REST Api is Code igniter + Phil Sturgeon Rest Client. More info:

Community
  • 1
  • 1
m4t1t0
  • 5,669
  • 3
  • 22
  • 30
  • Thanks. mod_rewrite works perfect for what I need to do. I really don't have time to learn a new framework right now so the simplest solution will do. I will look into frameworks farther ahead. Thanks! – tutiplain May 08 '13 at 19:57
0

In essence what you are looking for is something commonly referred to as request routing. Typically this is implemented by a combination of configurations at both the server level and app level.

  • Some sort of server redirect (using RewriteRule or FallBackResource directives) which directs all requests (or requests of a certain format) to single application resource which is to handle it.
  • Application logic to parse the URI and HTTPS action (GET, POST, PUT, DELETE, HEAD, etc.) and determine how to route the request (i.e. which controller should handle it)
  • Oftentimes there is some sort of logic to format the response according to the format requested in the request headers (i.e. JSON/XML/HTML)

There are a number of PHP frameworks available which do this, that at least you could look to for guidance (if not to just use them altogether).

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Mike Brant
  • 70,514
  • 10
  • 99
  • 103