Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a REST API From a PHP API

Tags:

rest

php

I am a beginner with PHP but I know most of the basics. I currently have an API for my website, coded myself, of which I can call different methods with different parameters and they will scour my databases for the relevant information.

However I want to convert it to REST.

So instead of having requests like this http://mywebsite.com/api/?param=allPosts I would have something like http://mywebsite.com/api/posts/. I would do this for each of my 23 different params.

How could I do this?

like image 389
max_ Avatar asked Nov 21 '25 14:11

max_


1 Answers

One way would be to use a micro framework for routing. This would 'point' url request patterns to relevant php files (controllers) to manage those requests and serve content (or perform CRUD operations, or whatever it is your API does).

There's a good post here with some discussion and further links.

https://stackoverflow.com/questions/115629/simplest-php-routing-framework

I'm currently in the process of using the framework Silex for this exact purpose

http://silex-project.org/

It may be that you wish to convert your PHP application to use one of the many frameworks out there (which will handle routing amongst other things).

The usual suspects are

  • CakePHP
  • Codeigniter
  • Symfony
  • Lithium

and there are many more...

like image 66
calumbrodie Avatar answered Nov 23 '25 02:11

calumbrodie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!