Since Slim is a small framework it’s main features are easily enumerable , these are Route matching, a Dependency injection container and Middleware support.
Route matching
In old school PHP applications routes are often created just by placing .php files with the desired name inside the public web directory. In Slim you have a single front controller that among other things specifies the routes of the application and maps them to specific callables:
$app = new \Slim\App();
$app->get('/hello/{name}', function (Request $request, Response $response, array $args) {
$response->getBody()->write("Hello, {$args['name']}");
return $response;
});
// other route definitions...
$app->run();
Dependency injection container
The Slim App object comes with a built-in DIC ($app->getContainer()) that you can leverage to define the relationship between the classes of your application. Another example that builds on the last one:
$app = new \Slim\App();
$cnt = $app->getContainer();
$cnt['pdo'] = function($cnt) {
return new \PDO('mysql:host=localhost;dbname=testdb', 'root', 'root');
};
$cnt['hello_action'] = function() {
return new \My\Namespace\HelloAction();
};
$cnt['register_action'] = function($cnt) {
return new \My\Namespace\RegisterAction($cnt['pdo']);
};
$app->get('/hello/{name}', 'hello_action');
$app->post('/register', 'register_action');
$app->run();
Notice that when defining a route the callable can be just the name of a service. Also, requests to /register will instantiate a \PDO object and open a database connection but requests to /hello/{name} will not (only the services that the matched route requires are actually executed).
Middleware support Middlewares are code that you can hook between Slim and the callables seen above. For instance, you could define a “backoffice” group of routes and attach an “AuthenticatedRequestsOnly” middleware to it. Or a “RateLimiting” middleware to the whole app, etc.
Edit: Here are a couple of demo projects showcasing all of the above: