PHP Restful API Slim Framework CRUD
Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs. (https://www.slimframework.com/)
you can Download SLIM Framework from Here.
- PHP + Mysql (With Mysqlnd extension)
- WAMP / XAMPP Server (I will be using wamp server)
- PHP IDE
- Postman to test our API: https://www.getpostman.com/apps
use book_db database
CREATE TABLE IF NOT EXISTS `book` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(35) NOT NULL,
`author` varchar(35) NOT NULL,
`price` double NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
First you need to create a PHP Project. You have to create it in the root directory of your server like it will be htdocs folder in case of xampp and www in case of wamp.
In my case C:\wamp\www\BookSlimAPI.
- Data: class representative our book table
- libs : All the third party libraries goes here. In our case we place Slim library here
- Manager: database business logic
- index.php : Takes care of all the API requests
- .htaccess : Rules for url structure and other apache rules
Description | Method | Route | Params |
---|---|---|---|
Creating a new book | POST | http://localhost/BookSlimAPI/api/book | title, author, price |
Listing all books | GET | http://localhost/BookSlimAPI/api/books | none |
Listing single book | GET | http://localhost/BookSlimAPI/api/book/{book_id} | none |
Deleting a book | DELETE | http://localhost/BookSlimAPI/api/book/{book_id} | none |
Updating a book | PUT | http://localhost/BookSlimAPI/api/book/{book_id} | id, title, author, price |