tags:

views:

1792

answers:

2

I need some better understanding on how to implement REST services with php

From what I have read sofar, they say "use logical adresses" and preferably don't use get variables at all.

What I don't see is how these url's are getting processed. Like www.somename.com/product and product is supposed to be not a physical address.

I mean, you do need some entry file that has the php processingcode that does the real work, don't I?

How does this work?

After that I can echo the data back between custom xml tags and the client app can parse this.That part I still remember from dealing with ajax. Only with REST you put all the resources(data or pieces off data) within a url.

So that takes me back to my original question. How are these logical url's being proccessed?? (within php that is)

Thanks, Richard

+1  A: 

The basic answer is something that someone mentioned in the above comments: mod_rewrite. By configuring Apache to use mod_rewrite to route your web requests, you can have all resource requests go through a single entry point. Example:

RewriteEngine On
RewriteRule !\.(js|ico|gif|jpg|png|css|html|swf|mp3|wav)$ index.php [L]

That sends all requests for a non-static resource to index.php. index.php can then make a decision on what content to load, say by examining $_SERVER['SCRIPT_URI'] and parsing out the fact that the request might have been /animals/cat.

Many MVC frameworks embrace this concept and utilize this routing method as a way to load the controllers and views.

zombat
thanks, why is this never mentioned in any tutorial?I am on a shared server by a host.In a zend framework.If it is not on, is there something I can do?
Richard
Often shared hosts will allow you to use .htaccess files, which allow you to specify configuration directives inside a directory when you don't have access to the main Apache configuration. You could try setting up a .htaccess file for your web root.
zombat
Ok, so this is the way REST works. Before I don't return to this question because I HAVE TO learn htaccess. Do you perhaps know off an example processingscript, link or otherwise to process logical adresses?
Richard
No, not off the top of my head. You might want to check out some of the popular frameworks to see how they do it. I'd recommend Code Igniter, they have a simple framework with good documentation. You could start here: http://codeigniter.com/user_guide/general/urls.html
zombat
so, I just tested a testscript for the mod_rewrite and the htaccess works.Does your rule mean,send everything threw except with those extensions. Or are thes rules more applicable.RewriteCond %{REQUEST_FILENAME} !-d // If the request is for a real directory go to that directoryRewriteCond %{REQUEST_FILENAME} !-f // If the request is for a real file go to that fileRewriteRule ^(.*)$ index.php?url=$1 [QSA,L] // This is where the magic happens//I am asking because I was not sure if you just wrote something down, just to show me how a rule works
Richard
A: 

IMO, your best bet for a RESTful web service in PHP is to use one of the existing frameworks that supports and understands RESTful-ness. Search for CakePHP, and CodeIgniter (and maybe Symfony, I'm not sure) all do the REST thing... none of them is perfect, so which one you choose will depend what your needs are. I've used Cake and CI, and I prefer CI.

These frameworks will use .htaccess and mod_rewrite if you're hosting on Apache, so take the previous answers to heart as well.

Trueblood