REST practicices on PHP, rewrite and http-verbs - php

I'm implementing a REST service in php.
q1. Can I split the controller and resource?
http://myserver/myCtrl.php?res=/items/1
q2. if not, is the standard specs (if any exists) for rewrites on iis, apache, nginx etc to survive the http-verb over the rewrite?
If not, how to solve?

For APIs (I have a framework for such) I tend to have a URL structure that looks as follows:
http://domain.com/api/[resource]/[id]/[subresource]
I pass all requests to a front controller with a .htaccess file that parses incoming requests and passes the request off to the relavant controller. So my index.php looks similar to the following at the very simplest:
<?php
$request = explode('/', trim($_SERVER['REQUEST_URI'], '/'));
$resource_name = ucfirst($request[0]).'Controller';
$http_verb = strtolower($_SERVER['REQUEST_METHOD']);
$controller = new $resource_name;
$response = call_user_func_array(array($controller, $http_verb), array($request));
header('Content-Type: application/json');
echo json_encode($response);
So if you call http://domain.com/api/news, then it will attempt to instantiate a class called NewsController, and if it's a GET request then the get() method of that class, or post() for a POST request, and so on. The response of that call is then returned to the client as JSON.
Hopefully that should be enough to get you started.

I had some of the same questions and found this video pretty helpful (not standards, but good practices):
http://blog.apigee.com/detail/slides_for_restful_api_design_second_edition_webinar/
I implemented my rest service by rewriting the urls through .htaccess files (mod_rewrite) and a central dispatcher so it looks like this:
http://myserver/myctrl/resource/1
My .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
</IfModule>
Read more about rewrite:
http://httpd.apache.org/docs/current/mod/mod_rewrite.html
I have an index file that does pretty much what Martin outlined. I explode on "/" and assume first is controller, second is action, and the rest are parameters.

Related

How to have clean URL's in PHP

Is there any way I can have good looking URL in PHP? any default php URL would look like this: http://example.com/something/?post=something But Is It possible to have It like this: http://example.com/something/user Is It possible to remove ?post= without using .htaccess
Here is some example code that I have been working on, Which on click of a post It would access my database and load the content:
<?php
if(!isset($_GET['post'])) {
$q = mysql_query("SELECT * FROM posts WHERE postID='something'");
} else {
$id = $_GET['post'];
$id = mysql_real_escape_string($id);
$q = mysql_query("SELECT * FROM posts WHERE postID='$id'");
}
$p = mysql_fetch_object($q);
?>
Thank you for your Time!
To get clean URLs you'll have to use mod_rewrite module, but you can minimize it's use, if you leave url parsing to your own script and have only one entry point. Look how it's made in WordPress:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
So if there's no real file or directory that is requested in URL, each and every request is redirected to index.php and then parsed by some internal route mapping script.
There's an example of such script in similar question.
I suggest for having clean URL use one of php frameworks which default have this ability.
if you want to use pure php you should use a Router in your project which means you should write your own php framework.
have you ever worked with any php framework??
I suggest using laravel or cakephp for entry point of learning php frameworks.
You need to define a Router and Dispatcher in your project.The Router extracts url and dispatcher calls that function related to url.in the other word,you should impelement frontcontroller design pattern in your project.I suggest check this tutorial
http://www.sitepoint.com/front-controller-pattern-1/

php router - variable url

Hello I was trying to come up with the solution to my problem, but I just was not able to. So here is my problem:
What I used was a .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.+(.*)/(.*)$ ./index.php?mesto=$1&den=$2 [QSA,NC,L]
It will display the url well. as www.site.com/CITY/DAY for example ../Prague/30.3.2014 but i need it to be more complex.
What I need is to have additional parameters, such as Bar or Restaurant in the url for example www-site.com/Prague/30.3.2014/p/bar/restaurant and other time I might have www-site.com/Prague/30.3.2014/p/pizza/bar
That part I have no idea how to do, because I have 5 different parameters
I imagine that the raw url would look this index.php?city=Prague&day=30.3.2014&p1=0&p2=0&p3=0&p4=0&p5=0 where p1 to p5 are the parameters being active (0 not 1 yes).
I don't understand how to detect what parameters are active and how to properly display the pretty url. Could you please help me?
Use
RewriteRule ^(.*)$ ./index.php [QSA,NC,L]
This will redirect all your requests to a single index.php that parses the uri with something like this:
<?php
// Example URI: /florence/30-06-2009
// Remove first slash from REQUEST_URI
$uri = substr($_SERVER['REQUEST_URI'],1);
// Get an array with portions between slashes.
$splittedURI = explode("/", $uri);
// Here you get your city (or anything else that you want)
$city = array_unshift($splittedURI); // In example, $city = "florence"
// Remaining itens in $splittedURI are the arguments/parameters to your page
// Like this:
$date = $splittedURI[0]; // In example, $date = "30-06-2009"
?>
Remember that this is just and example, and you should do additional verifications to avoid PHP exceptions.
If you need complicated routing (and if you sure you want to create your own router instead of using a ready solution as ZF, Symfony etc.) you're better off just passing the whole request uri to a php router object. There you can as complex router logic as you need.
So basically, loose the parsing in the rewrite rule:
RewriteRule ^(.*)$ ./index.php?route=$1 [QSA,NC,L]
Then you can let the index.php create a router object that can parse the route parameter and delegate the job where it needs to.
I'd recommend reading up about existing routing solutions though.

Vanity URL Creation

I've been looking for an answer but I just can't find one. I'm not looking for a way for a user on my site to be able to quickly get to their page, and quite frankly, the same question keeps popping up, "How to Create a Facebook Profile URL" or, "How to use ".htaccess". I'm just looking for a simple bit of PHP that can create basic vanity URL's so I can quickly access different pages on my site and make it look more neat.
Put this in .htaccess to send all requests to index.php (this is called a front controller)
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^.*$ ./index.php
Then put this in your index.php
<?php
// get the request
$request = $_SERVER['REQUEST_URI'];
// split the path by '/'
$params = split("/", $request);
You now can request a page like: http://foo.com/one/two/three
Your $params variable will now look like:
$params = array('', 'one', 'two', 'three');
You can now use those parameters to call functions or redirect to pages or whatever you want.
This is a very simplified example but it should give you the basic idea.
PHP doesn't support vanity URLs at all, you need to configure your webserver to use them.
.htdocs are configuration files for apache, however, your case may be:
Apache hasn't been configured to process these.
Your server isn't apache httpd.

Easy mod_rewrite - So I'll never have to think about it again

Not sure how you'll take this question but...
Whenever I try to make my URLs look pretty I always end up messing around for too long and it's simply not worth the trouble. But the end effect is good if it were a simple task.
So what I want to do is create a method which in the end would achive something like...
index.php?do=user&username=MyUsername //This becomes...
/user/MyUsername //...that
index.php?do=page&pagename=customPage //And this becomes...
/page/customPage //...that
index.php?do=lots&where=happens&this=here //This also becomes...
/lots/happens/here //...that
index.php?do=this&and=that&that=this&and=some&more=too //And yes...
/this/that/this/some/more //This becomes this
So then I just make a nice .htacess file that I'll never have to look at again. Everything will be better in the world because we have pretty URLs and my head didn't hurt in the making.
You can use a different approach of throwing the url in a single parameter, and parse it in your application.
So the apache rewrite rule would look like:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
which will convert your urls as follows:
/user/MyUsername => index.php?q=/user/MyUsername
/page/customPage => index.php?q=/page/customPage
...
In your app, you then have a $_GET['q'] variable, which you can split by '/', and have your arguments in order. In PHP it would be something like:
$args = explode('/', $_GET['q']);
$args will be an array with 'user', 'MyUserName', etc.
This way you will not have to touch your .htaccess again, just your app logic.
For /user/MyUsername ==> index.php?do=user&username=MyUsername and /page/customPage ==>
index.php?do=page&pagename=customPage, you can use:
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)$ index.php?do=$1&$1name=$2 [L]
But I don't think you can write a catch-all rule for /lots/happens/here and /this/that/this/some/more because you need to tell mod_rewrite how to translate the two urls.
Remember, mod_rewrite has to translate /lots/happens/here into index.php?do=lots&where=happens&this=here and not the other way around.
The best approach would be to delegate your application to generate the “pretty URLs” as well as parse and interpret them and to use mod_rewrite only to rewrite the requests to your application with a rule like this one:
RewriteRule %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
This rule will rewrite all requests that can not be mapped directly to an existing file to the index.php. The originally requested URL (more exact: the URL path plus query) is then available at $_SERVER['REQUEST_URI'].

How to implement URL pattern interpreter as used by Django and RoR in PHP

What's the best way to implement a URL interpreter / dispatcher, such as found in Django and RoR, in PHP?
It should be able to interpret a query string as follows:
/users/show/4 maps to
area = Users
action = show
Id = 4
/contents/list/20/10 maps to
area = Contents
action = list
Start = 20
Count = 10
/toggle/projects/10/active maps to
action = toggle
area = Projects
id = 10
field = active
Where the query string can be a specified GET / POST variable, or a string passed to the interpreter.
Edit: I'd prefer an implementation that does not use mod_rewrite.
Edit: This question is not about clean urls, but about interpreting a URL. Drupal uses mod_rewrite to redirect requests such as http://host/node/5 to http://host/?q=node/5. It then interprets the value of $_REQUEST['q']. I'm interested in the interpreting part.
If appropriate, you can use one that already exists in an MVC framework.
Check out projects such as -- in no particular order -- Zend Framework, CakePHP, Symfony, Code Ignitor, Kohana, Solar and Akelos.
have a look at the cakephp implementation as an example:
https://trac.cakephp.org/browser/trunk/cake/1.2.x.x/cake/dispatcher.php
https://trac.cakephp.org/browser/trunk/cake/1.2.x.x/cake/libs/router.php
You could also do something with mod_rewrite:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*)$ $1 [L]
RewriteRule ^([a-z]{2})/(.*)$ $2?lang=$1 [QSA,L]
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
This would catch urls like /en/foo /de/foo and pass them to index.php with GET parameters 'lang' amd 'url'. Something similar can be done for 'projects', 'actions' etc
Why specifically would you prefer not to use mod_rewrite? RoR uses mod_rewrite. I'm not sure how Django does this, but mod_php defaults to mapping URLs to files, so unless you create a system that writes a separate PHPfile for every possible URL (a maintenance nightmare), you'll need to use mod_rewrite for clean URLs.
What you are describing in your question should actually be the URL mapper part. For that, you could use a PEAR package called Net_URL_Mapper. For some information on how to use that class, have a look at this unit test.
The way that I do this is very simple.
I use wordpress' .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
What this .htaccess does is when something returns a 404, it sends the user to index.php.
In the above, /index.php is the "interpreter" for the URL.
In index.php, I have something along the lines of:
$req = $_SERVER['REQUEST_URI'];
$req = explode("/",$req);
The second line splits up the URL into sections based on "/". You can have
$area = $req['0'];
$action= $req['1'];
$id = $req['2'];
What I end up doing is:
function get_page($offset) {//offset is the chunk of URL we want to look at
$req = $_SERVER['REQUEST_URI'];
$req = explode("/",$req);
$page = $req[$offset];
return $page;
}
$area = get_page(0);
$action = get_page(1);
$id = get_page(2);
Hope this helps!
Just to second #Cheekysoft's suggestion, check out the Zend_Controller component of the Zend Framework. It is an implementation of the Front Controller pattern that can be used independently of the rest of the framework (assuming you would rather not use a complete MVC framework).
And obviously, CakePHP is the most similar to the RoR style.
I'm doing a PHP framework that does just what you are describing - taking what Django is doing, and bringing it to PHP, and here's how I'm solving this at the moment:
To get the nice and clean RESTful URLs that Django have (http://example.com/do/this/and/that/), you are unfortunately required to have mod_rewrite. But everything isn't as glum as it would seem, because you can achieve almost the same thing with a URI that contains the script's filename (http://example.com/index.php/do/this/and/that/). My mod_rewrite just forwards all calls to that format, so it's almost as usable as without the mod_rewrite trick.
To be truthful, I'm currently doing the latter method by GET (http://example.com/index.php?do/this/and/that/), and fixing stuff in case there are some genuine GET variables passed around. But my initial research says that using the direct slash after the filename should be even easier. You can dig it out with a certain $_SERVER superglobal index and doesn't require any Apache configuration. Can't remember the exact index off-hand, but you can trivially do a phpinfo() testpage to see how stuff look like under the hood.
Hope this helps.

Categories