Using Clean Urls in Restful API

Using Clean URLs in RESTful API

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

RewriteEngine On
RewriteRule ^/api/ api.php

api.php

$request = $_SERVER['REQUEST_URI'];  //this would be /users/show/abc.json

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

RewriteEngine On
RewriteRule ^/api/([^\.]+).(xml|json|atom) api.php?url=$1&type=$2

api.php

$request_parts = explode('/', $_GET['url']); // array('users', 'show', 'abc')
$file_type = $_GET['type'];

$output = get_data_from_db(); //Do your processing here
//You can outsource to other files via an include/require

//Output based on request
switch($file_type) {
case 'json':
echo json_encode($output);
break;
case 'xml':
echo xml_encode($output); //This isn't a real function, but you can make one
break;
default:
echo $output;
}

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

  • mod_rewrite, a beginner's guide
  • Create a REST API with PHP
  • Recess PHP Framework | Tutorials

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!

Rewrite URLs in RESTful API

Actually you should store the .htaccess on the root directory, otherwise Apache will not know that there's an .htaccess under /src/api/v1, so apache will look for the directory src/api/v1/posts and as is it's not found a 404 error will be returned.

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule src/api/v1/(.*)$ /src/api/v1/api.php?request=$1 [QSA,NC,L]
</IfModule>

yii2 rest api url rewriting (hide directories)

   'request' => [
'baseUrl' => str_replace('/api/web', '', (new \yii\web\Request())->getBaseUrl()),

],

Add this request component config in your main api config

How to access query string parameters after Cleaning your PHP REST API?

You can use these rules:

RewriteEngine On

RewriteRule ^(?:action|api\.php)/([^/.]+)/?$ api.php?action=$1 [L,NC,QSA]


Related Topics



Leave a reply



Submit