Build a Modern API with Slim 4 - Create your Index File
Tags: tutorial, api, php
Published: 2023-02-26 13:00:00

Now that we have everything set up we can actually start creating our index file which will handle all incoming requests and return the relevant responses.

We first start off by importing the relevant classes from the packages we installed, this is done with the `use` statement:

```php
<?php
use DI\Container;
use Slim\AppFactory;
// Expect this list to get much, much longer as we progress!

require __DIR__ . '../vendor/autoload.php';
// This script is generated by Composer and brings in the code from all the classes listed above
```

Now we will create our `$container` and `$app` objects, the container is what will handle our dependency injection and the app is what will actually listen out for and handle the requests and responses.

```php
$container = new Container();

AppFactory::setContainer($container);
$app = AppFactory::create();
```

You may think that we haven't actually done much to the container thus far, and you'd be correct. We will soon be setting different properties on it in order to allow our app to access them.

One major part of any web-based application, be it an API, website or webapp, is middleware; middlewares are what we use to manipulate requests coming into the server before they reach our controllers, as well as responses leaving the server before they reach the user.

The order is which middleware is applied varies in importance, there are some where it doesn't matter where they go in the stack and there are others that *have* to be below or above certain others. For example if your authentication middleware adds the `user_id` to the request, then any middleware that relies on that ID must be below it.

For this reason, middleware is applied in what I'll call a layered approach, the outermost middleware is what the request will first encounter, and what its response will last encounter. All midlewares have the capability to manipulate both requests and responses, although I have found it typical to write them in such a way that they only do one or the other.

![Slim's diagram of how middleware operates](https://www.slimframework.com/docs/v4/images/middleware.png)
*Slim's diagram of how middleware operates, from the documentation*

We will talk about writing our own middlewares later in this series, but for now we will simply add some already built-in to the Slim framework:

```php
$app->addBodyParsingMiddleware(); // This enables us to easily access the contents of, say, a JSON body or form submission
$app->addRoutingMiddleware(); // This lets us access the route of a request and see where it is going and what it is trying to do
$app->addErrorMiddleware(true, true, true); // This handles errors and returns the relevant messages.
// The three parameters above determine whether to display the error details to the user (fine in development, not in production), log the error, and log the details of the error.
```

The last middleware added is the outermost layer, so in this case we have our app wrapped in the body parsing middleware, wrapped in the routing middleware, wrapped in the error middleware. It is wise to have the error middleware as your outermost layer, and Slim's documentation reflects this.

We now have one final line of this script, the most important of all; actually starting the application. This is done quite simply with `$app->run();`, leaving us with the below file:

```php
public/index.php

<?php
use DI\Container;
use Slim\AppFactory;

require __DIR__ . '/../vendor/autoload.php';

$container = new Container();

AppFactory::setContainer($container);
$app = AppFactory::create();

$app->addBodyParsingMiddleware();
$app->addRoutingMiddleware();
$app->addErrorMiddleware(true, true, true);

$app->run();
```