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
-
3Just something you should read before calling your app restful: roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-drivenGumbo– Gumbo2009年07月22日 15:11:24 +00:00Commented Jul 22, 2009 at 15:11
-
@Gumbo, I haven't even started I read this also xfront.com/REST-Web-Services.html and somewhere in the middle it says, it does not deal with the implementation, but I still would very much like to know.Richard– Richard2009年07月22日 15:21:09 +00:00Commented Jul 22, 2009 at 15:21
-
Yeah, Gumbo's comment is very important. REST has nothing to do with logical URI naming conventions.aehlke– aehlke2009年07月22日 19:37:26 +00:00Commented Jul 22, 2009 at 19:37
2 Answers 2
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.
-
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– Richard2009年07月22日 15:38:33 +00:00Commented Jul 22, 2009 at 15:38
-
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– zombat2009年07月22日 15:44:45 +00:00Commented Jul 22, 2009 at 15:44
-
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– Richard2009年07月22日 16:12:01 +00:00Commented Jul 22, 2009 at 16:12
-
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: codeigniter.com/user_guide/general/urls.htmlzombat– zombat2009年07月22日 16:47:52 +00:00Commented Jul 22, 2009 at 16:47
-
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 directory RewriteCond %{REQUEST_FILENAME} !-f // If the request is for a real file go to that file RewriteRule ^(.*)$ 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 worksRichard– Richard2009年07月22日 17:07:05 +00:00Commented Jul 22, 2009 at 17:07
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.