An overview of Slim Micro Framework

  • Cubettech
  • Laravel Development
  • 9 years ago
An overview of Slim Micro Framework

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: $app->run(); ?>

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->redirect('/login');        }    }; }; $app = new \Slim\Slim(); $app->get('/my_route', $authenticateForRole('admin'), function () {    //Display admin control panel }); // run $app->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

Got a similar project idea?

Connect with us & let’s start the journey!

Questions about our products and services?

We're here to support you.

Staff augmentation is a flexible workforce strategy companies adopt to meet specific project needs or address skill gaps.

Begin your journey!
Need more help?