0

I want to create a pure PHP REST API and I am quite new in backend development field, but I am experienced software developer, so some concepts are known to me.

However, after watching several tutorials on how to create REST API with PHP, all instructors were using simpler examples, where no nesting exists.

Simple Example:

GET /api/category/read.php

However, I want to create something like this:

GET /api/{user_id}/{folder_id}/{file_name}/read.php

I am struggling to find any tutorial covering this with PHP. And I have spent several hours trying to figure it out by myself by trying to modify the code I have seen in Tutorial videos. I mean if I do like they do, this would mean manually creating folders in my Project Folder for each {user_id} and so forth for each sub-folder... but I do not think that such hardcoding is the solution.

I have found some SO questions here relating closely to my question, but none have satisfying answers - makes me wonder that this if this is possible to do at all. But it seems so common (for example, I know that GitHub API has just that support /{user}/repos) so I think it should be doable.

I would be really grateful if someone could help me out how to accomplish my goal. If not else, pointing to a tutorial / documentation that does just that is equivalently appreciated!

AndroidDev101
  • 122
  • 1
  • 13
  • You need to implement `url rewrite`. Which web server you use? Apache/nginx/iis? – Seva Kalashnikov Sep 28 '18 at 17:09
  • May I invite you to look at my work https://github.com/funilrys/funombi ? It's still in developpement but you may found the `Core\Router()` and the `public/index.php` useful ... – funilrys Sep 28 '18 at 17:21
  • @SevaKalashnikov I will have to look into that. Because I am develop backend only app. No frontend is there. I just need to provide working REST API. I use Apache through XAMPP. – AndroidDev101 Sep 28 '18 at 17:22
  • @funilrys I will take a look, but I assume that this is connected with frontend development too? As I won't be developing any frontend code. Just REST API. – AndroidDev101 Sep 28 '18 at 17:22
  • @AndroidDev101 Well it's an MVC "framework" but you can do a REST API with it as it can be used for back- and frontend development. I actually wrote some big private project with that "framework" ... Feel free to open an issue there if you do not understand something. – funilrys Sep 28 '18 at 17:26
  • Have you tried to use a php FrameWork like Symfony ? Its very handly to develop a RESTFULL API. It will take some time to learn but very interesting. – Cheikh HAIBALA Sep 28 '18 at 17:29
  • @CheikhHAIBALA I read that it's fairly easy to do it PHP Frameworks, but wanted to try it with pure PHP. I will see what I will be able to do. – AndroidDev101 Sep 28 '18 at 17:50

2 Answers2

1

You do not need to create the folder structure to achieve this. It would be more advantageous to use something like Apache Mod Rewrite or a framework like Laravel to help avoid the need to create the file structure you are describing and have a single endpoint for handling specific routes:

Using mod rewrite with Apache2 would work something like:

.htaccess

RewriteEngine On
RewriteBase /
RewriteRule ^api/(.+)/(.+)/(.+)/read /api/read.php?user_id=$1&folder_id=$2&file_name=$3

This would provide the URI variables in the $_GET and $_REQUEST supergobals in /api/read.php


Using the Laravel framework you can leverage their MVC approach and create dynamic routes which can capture the URL vars and deliver them to the desired Controller endpoint:

in your routes file:

Route::get('api/{user_id}/{folder_id}/{file_name}/read', Controller@read)

in the Controller:

public function read(user_id,folder_id,file_name){ /* do stuff */ }

There is alot more to know about the specifics of MVC and using Laravel to create an API, however, they have great documentation and tutorials.

M31
  • 1,348
  • 12
  • 16
  • Indeed I have encountered that with PHP frameworks, this can get fairly easy to implement. But I really want to force me use vanilla PHP for this task. I will dig a bit more into some documentation and try to do it, but if all fails, I will accept your answer for Laravel. – AndroidDev101 Sep 28 '18 at 17:49
  • I highly recommend Laravel since it takes care of a lot of the little details for you and provides a lot of functionality up front. But there is no reason you can't achieve the same effect with a careful application of Mod Rewrite and your own routing implementation. – M31 Sep 28 '18 at 17:52
0

Create a PHP script that receives every request (have Apache direct all requests to it), and then process the $_SERVER['REQUEST_URI'] variable to split the path into segments, storing the parts in variables of your choice. Then dispatch the request to sub-components as necessary.

Nicholas Shanks
  • 10,623
  • 4
  • 56
  • 80