Key Takeaways
- Slim Framework is lightweight, fast, and ideal for REST API and small web applications.
- Supports all major HTTP methods: GET, POST, PUT, DELETE.
- Comes with built-in tools like routers, middlewares, body parser, flash messages, and encrypted cookies.
- Simplifies rapid PHP web app development without the overhead of full-stack frameworks.
- Perfect for developers looking for easy-to-learn, scalable, and maintainable PHP framework.
Developing APIs and web applications quickly can be challenging without the right tools. PHP Slim Framework offers a lightweight and fast solution for building REST APIs and small web applications. Unlike heavier frameworks like Laravel, Symfony, or CodeIgniter, Slim PHP Framework provides an easy-to-learn approach with minimal boilerplate, ideal for rapid development.
In this tutorial, we’ll explore how to install Slim Framework, create a REST API for a book library, and perform CRUD operations using GET, POST, PUT, and DELETE requests.
What is Slim Framework?
Slim is super lightweight framework, ideal for rapid web app development. One of the important usages is in REST API development. Slim supports all HTTP method (GET,POST,PUT,DELETE). Slim contains very handy URL structure with routers, middlewares, bodyparser along with page templates, flash messages, encrypted cookies and lots more.
At this point, it is important to understand the structure of the REST API.
Related: Creating Twig Templates in Slim Microframework
Understanding REST API in Slim Framework
REST is the abbreviation of Representational State Transfer. This is a bridge or medium between data resource and application interface, whether it’s on mobile devices or desktops. REST provides a block of HTTP methods which are used to alter the data. The following are common HTTP methods:
| GET | is used for reading and retrieving data. |
| POST | is used for inserting data. |
| PUT | is used for updating data. |
| DELETE | is used for deleting data. |
Basically, REST phenomena works on actions and resources. Whenever any action URL is invoked, it performs an individual method (or a set of methods) on that URL. I will further discuss this below with examples.
First we will need to install Slim framework for the REST API project.
I assume that you already have your Cloudways server launched with PHPstack and if you didn’t launch your server signup to get it.
Host PHP Websites with Ease [Starts at $10 Credit]
- Free Staging
- Free backup
- PHP 8.0
- Unlimited Websites


After creating the server launch SSH terminal.

Step 1: Install Slim Framework via Composer
Open SSH terminal from the Cloudways panel and and sign in with your username and password. Now go to the folder where you want to install SLIM with cd command

Input the following command in the terminal to install Slim via composer.
composer require slim/slim"^3.0"

After installing Slim, the following piece of code will require it in the index.php file to require autoload file and instantiate Slim.
<?php require 'vendor/autoload.php'; $app = new Slim\App();
Composer comes pre-installed on Cloudways servers. If you are working on the localhost, you need to install it. If you haven’t installed it yet, just go to the following link and follow the instructions.
When it comes to working with Composer and PHP, having a reliable hosting solution is crucial. While Composer comes pre-installed on Cloudways servers, if you’re working on localhost, you’ll need to install it yourself. If you haven’t installed Composer yet, it’s important to find the best hosting for PHP that provides easy installation options and support for this tool. By choosing a reliable hosting solution that supports Composer, you can ensure that you have the tools you need to manage your PHP dependencies and keep your application running smoothly.
Nothing as Easy as Deploying PHP Apps on Cloud
With Cloudways, you can have your PHP apps up and running on managed cloud servers in just a few minutes.
Step 2: Configure .htaccess for Clean URLs
To make your life easier, you should create a .htaccess file that defines clean URL structure. At the root directory, make a .htaccess file and add the below code in it. This will provide a clean URL structure for the PHP file. (this just means that you don’t want to include PHP filename in the URL calls).
RewriteEngine On
RewriteCond %{Request_Filename} !-F
RewriteCond %{Request_Filename} !-d
RewriteRule ^ index.php [QSA,L]
If your index file is located in different folder (for instance, the “public” folder), then you can insert the full path of the index file in the last line:
RewriteRule ^ public/index.php [QSA,L]
Step 3: Create a MySQL Database
With each PHP Stack on Cloudways, you get an empty database.

Click on Launch Database Manager. To create the requisite tables, run the following query in SQL Command box:

CREATE TABLE IF NOT EXISTS `library` ( `book_id` int(11) NOT NULL, `book_name` varchar(100) NOT NULL, `book_isbn` varchar(100) NOT NULL, `book_category` varchar(100) NOT NULL ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8; INSERT INTO `library` (`book_id`, `book_name`, `book_isbn`, `book_category`) VALUES (1, 'PHP', 'bk001', 'Server Side'), (3, 'javascript', 'bk002', 'Client Side'), (4, 'Python', 'bk003', 'Data Analysis');
Now it is time for the first API call. Let’s make it systematically.
You might also like: Using Eloquent ORM With Slim
Step 4: Retrieve Records with GET Request
Enter the following code in the index.php file to get all the books from the database. A GET call is used for retrieval.
$app->get('/books', function() {
require_once('db.php');
$query = "select * from library order by book_id";
$result = $connection->query($query);
// var_dump($result);
while ($row = $result->fetch_assoc()){
$data[] = $row;
}
echo json_encode($data);
});
To streamline working with the API calls, I recommend using Postman (available from the Chrome App Store). This plugin greatly helps in API management and usage.
In postman, make a GET call with API URL.

Step 5: Insert Records with POST Request
Make a new API call in the index.php through the following code:
$app->post('/books', function($request){
require_once('db.php');
$query = "INSERT INTO library (book_name,book_isbn,book_category) VALUES (?,?,?)";
$stmt = $connection->prepare($query);
$stmt->bind_param("sss",$book_name,$book_isbn,$book_category);
$book_name = $request->getParsedBody()['book_name'];
$book_isbn = $request->getParsedBody()['book_isbn'];
$book_category = $request->getParsedBody()['book_category'];
$stmt->execute();
});
Open Postman and click Body. Select x.www-form-urlencoded. Now add records to insert via POST call.

Step 6: Update Records with PUT Request
Make a new API call like below to update a record in the database.
$app->put('/books/{book_id}', function($request){
require_once('db.php');
$get_id = $request->getAttribute('book_id');
$query = "UPDATE library SET book_name = ?, book_isbn = ?, book_category = ? WHERE book_id = $get_id";
$stmt = $connection->prepare($query);
$stmt->bind_param("sss",$book_name,$book_isbn,$book_category);
$book_name = $request->getParsedBody()['book_name'];
$book_isbn = $request->getParsedBody()['book_isbn'];
$book_category = $request->getParsedBody()['book_category'];
$stmt->execute();
});
In Postman, add data to update a specific book record.

Step 7: Delete Records with DELETE Request
To delete a record with a specific ID, a DELETE call is required.
$app->delete('/books/{book_id}', function($request){
require_once('db.php');
$get_id = $request->getAttribute('book_id');
$query = "DELETE from library WHERE book_id = $get_id";
$result = $connection->query($query);
});
On Postman, run the call like this

This is all for the basic REST API in the Slim Framework. However, this API will not work until you add this command at the end of the code.
$app->run();
Supercharged Managed PHP Hosting – Improve Your PHP App Speed by 300%
Conclusion
Creating a REST API with Slim PHP Framework is straightforward and efficient. Its lightweight nature and simplicity make it ideal for developers who need rapid application development without sacrificing flexibility.
With Slim Framework, you can handle HTTP requests, perform CRUD operations, and build maintainable APIs with minimal setup. Pair it with a reliable PHP hosting provider like Cloudways to speed up deployment and streamline development.
Start building your REST APIs today with Slimframework and enjoy faster, simpler, and scalable PHP development.
Frequently Asked Questions
1. What is Slim Framework in PHP?
Slim Framework is a lightweight PHP microframework designed for building REST APIs and small web applications quickly and efficiently.
2. How do I create a REST API using Slim PHP Framework?
Install Slim via Composer, set up routing, configure database connections, and define GET, POST, PUT, and DELETE endpoints.
3. Is Slim Framework suitable for large applications?
Slim is ideal for small to medium applications or API backends. For large-scale apps, you might integrate it with other PHP libraries or frameworks.
4. Can I use Slim Framework for CRUD operations?
Yes. Slim supports all HTTP methods and can easily handle CRUD operations in REST APIs.
5. Do I need a database to use Slim Framework REST API?
While Slim can run without a database, integrating MySQL or another database is common for handling dynamic data.
6. What makes Slim PHP Framework different from Laravel or Symfony?
Slim is lightweight, minimal, and faster for small projects. Unlike Laravel or Symfony, it has a shallow learning curve and focuses on microservices and REST APIs.
Shahzeb Ahmed
Shahzeb is a Digital Marketer with a Software Engineering background, works as a Community Manager — PHP Community at Cloudways. He is growth ambitious and aims to learn & share information about PHP & Laravel Development through practice and experimentation. He loves to travel and explore new ideas whenever he finds time. Get in touch with him at [email protected]