An overview of Slim Micro Framework

April 07, 2015

software development services

The Slim Framework is a PHP micro framework that helps PHP developers quickly and easily write web applications and API’s. We can find attractive and distinct features like Url routing, middleware architecture etc.These features makes slim ideal for Website development or API prototyping .


1. Full featured and open-source.
2. Supports all HTTP methods GET, POST, PUT and DELETE which are necessary for a REST API.
3. Middleware architecture.
4.Simple Configuration.
6.Flexible Routing.


Installing Slim Framework consists of three steps

1. Download Slim Framework

We can download the Slim Framework using the following command:


Can download from
Then place the zip file in your project folder.

2. Extract from the Zip File

The contents of the zip file can be extracted using the following command:
unzip master -d .
If you get any error while installing, you can use this command  “apt-get install” unzip and then we can execute the above command to extract all the files. Extracted folder contains a folder named Slim which is the framework folder.

3. Copy Slim Framework to a Common Location

Copy the Slim folder to your project folder /usr/local/project/Slim.
Main routing methods:

1.GET Routes

get('/books/:id', function ($id) {
    //Show book identified by $id

In this example,the http GET request with id as request parameter.You have to pass the argument along with the route as given here: /books/:id.

2.POST Routes

post('/books', function () {
    //Create book

In this example, an HTTP POST request for “/books” will invoke the associated callback function.

3.PUT Routes

put('/books/:id', function ($id) {
    //Update book identified by $id

In this example, the http PUT request with id as request parameter.You have to pass the argument along with the route like this /books/:id.

4.DELETE Routes

delete('/books/:id', function ($id) {
    //Delete book identified by $id

In this  example,the http DELETE request with id as request parameter.

Simple Example

get('/my_route/:name', function ($name) {
    echo "Hey, $name";
Run the Slim Application:

In this example you can call the route like /usr/local/project/Slim/my_route/TestName
Output will be like this:

“Hey TestName”

Adding authentication with route middleware

As we discussed slim supports routing, Slim also supports “Middleware”. Middleware can be defined as one or more custom functions that are invoked before the callback function for the corresponding route. With middleware, we can ensure that the API requests are properly authenticated by passing the request through a custom authentication method before allowing them to proceed.

Authentication middleware
belongsToRole($role) === false ) {
            $app = \Slim\Slim::getInstance();
            $app->flash('error', 'Login required');
$app = new \Slim\Slim();
$app->get('/my_route', $authenticateForRole('admin'), function () {
    //Display admin control panel
// run

In the above example, when the user  requests the URL “/my_route'”, authenticateForRole() will be invoked.This function will authenticate the request , and the further processing will be allowed when  function returns “true”. In case if validation fails, the authenticateForRole() halts processing and sends the client a 401 Authorization Required response.In this case, based on the role specified, the request will decide further processing. The function will return true, when request role is ‘admin’;otherwise will exit the process. So with this simple code we can ensure the authentication. If you need to authenticate any request, just add this authenticateForRole() with that route.

Know More About This Topic from our Techies

What Are You Missing in Your Web Application Project and Why Do We Recommend Laravel
Why Laravel Application Development Is Not a Piece of Cake for Everyone
Why Are Web Applications Necessary for Emerging Businesses?
Creating animations in Flutter by using Rive
Flutter's Animation Support
Motion UI SaaS Library for Creating Flexible UI And Transitions

Latest Post

Get a free quote