$app->get('/hello/{name}', function ($request, $response, $args) { return $response->write("Hello, " . $args['name']); });
$app->add(function ($request, $response, $next) { $response = $next($request, $response); $response = $response->withHeader('X-Powered-By', 'Slim'); return $response; });
$app->get('/users', function ($request, $response, $args) use ($container) { $users = $container->get('user_repository')->getAll(); return $response->withJson($users); });In this example, we use Slim's built-in support for dependency injection to fetch a user repository object from the container and use it to retrieve a list of all users. We use the ->get() method of the container object to retrieve the repository instance, and then pass it to the closure using the "use" keyword. Package library: Dependency injection Overall, Slim's simplicity and flexibility make it a great choice for building web applications of any size. Its collection of package libraries like routing, middleware, and dependency injection make it an even more powerful tool for developers.