Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

Project structure

Slim does not prescribe a project structure: an application in one file works the same as one split into folders. But an index.php hundreds of lines long quickly becomes unmanageable. In this lesson the code from chapter one moves into several files — the rest of the course builds on this layout.

Folders

Create the config and src folders by hand next to public:

blog/
├── config/
│   ├── bootstrap.php   ← builds the application
│   └── routes.php      ← all routes
├── public/
│   └── index.php       ← entry point
├── src/                ← application classes
├── vendor/
└── composer.json
FolderWhat it holds
public/Only what the browser may reach
config/Files that build and configure the application
src/Your classes: actions, middleware, database code

Later lessons add templates/ for templates and var/ for cache and logs.

Class autoloading

To have classes from src load by themselves, without require, add an autoload section to composer.json:

{
    "require": {
        "slim/slim": "4.*",
        "slim/psr7": "^1.7"
    },
    "autoload": {
        "psr-4": {
            "App\\": "src/"
        }
    }
}

You already have the require section — only autoload is added. Then run:

composer dump-autoload

Now a rule applies: the App namespace maps to the src folder. The class App\Action\HomeAction must live in the file src/Action/HomeAction.php.

The routes file

Create config/routes.php. The file returns a function that receives the application and declares routes on it:

// config/routes.php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\App;
 
return function (App $app): void {
    $app->get('/', function (Request $request, Response $response) {
        $response->getBody()->write('Blog home page');
 
        return $response;
    })->setName('home');
};

All routes from public/index.php move here.

The bootstrap file

Create config/bootstrap.php. It creates the application, loads the routes and returns the finished object:

// config/bootstrap.php
use Slim\Factory\AppFactory;
 
require __DIR__ . '/../vendor/autoload.php';
 
$app = AppFactory::create();
 
(require __DIR__ . '/routes.php')($app);
 
return $app;

The require line reads: load the file, get the function from it and call it at once with $app.

The entry point

Two lines remain in public/index.php:

// public/index.php
$app = require __DIR__ . '/../config/bootstrap.php';
 
$app->run();

Start the server with the same php -S localhost:8080 -t public command and open the home page — it shows "Blog home page". The application works as before, but building is separated from running: later the tests will load the same bootstrap.php.

Common mistake

After editing the autoload section of composer.json you must run composer dump-autoload, otherwise PHP reports Class "App\..." not found. The same error appears when a file or folder name does not match the class name and namespace — down to letter case: on Linux src/action and src/Action are different folders.

Thanks for reading!

Donate to the site — we work hard for you.