Slim Framework 3.x entered beta in July 2015. This presentation introduces you to the main components of the framework, including the router, request and response objects, middleware, and dependency injection. The next best thing will be the docs on Slim's website, which are still a work in progress for 3.x, and the code itself - it's not hard to follow and is designed to be read.
3. Created by Josh Lockhart (a.k.a. @codeguy),
author of Modern PHP and PHP the Right Way.
http://www.phptherightway.com
4. A BRIEF HISTORY OF SLIM
• Started as a learning experience /
tool for its creator. Sound familiar?
• Nov. 2, 2010 - Slim 1.0.0
• Sep. 9, 2012 - Slim 2.0.0
• @silentworks / @akrabat added as
maintainers to grow its bus factor
• PHP-FIG accepts PSR-7 (HTTP
Request / Response interfaces)
• July 2, 2015 - Slim 3.0.0-beta1
5. THE SLIM APPROACH
• Quickly define an application with all of its routes.
• Match incoming requests to routes that generate
responses using PSR-7 request / response objects.
• Add application and route level “middleware”
callbacks as LIFO / FILO stacks. #yolo
10. GROKKINGTHE ROUTE
• A route is a closure or callable with a signature
that includes the request, response, and arguments.
It MUST return a response object.
• Route closures are bound to the $app object.
• Output buffering captures all echoed output to be
returned as the response body.
11. A BETTER ROUTE
• Use the response object’s write method (and give
the route a name):
$app->get('/hello', function($request,
$response, $args) {
$response->write('Hello, world!');
return $response;
})->setName('hello-world');
12. USING NAMED ARGUMENTS
• The Slim router extends FastRoute, which includes
support for named arguments (e.g. node/%node).
• Curly braces denote an argument with support
for optional matching via regular expressions.
• Arguments are passed to the route callback via
the $args associative array.
15. GROKKING MIDDLEWARE
• Middleware is a callable added to the application
and individual routes with a signature that includes
the request, response, and next callable.
• Middleware MUST return a response object.
• Middleware MAY invoke the next callable in the
stack and may execute more code after doing so.
19. LAYERING MIDDLEWARE
• Use middleware to manage user sessions and
authentication, content negotiation, caching, etc.
(From http://stackphp.com.)
20. MANAGING DEPENDENCIES
• Slim uses the “Pimple” dependency injection container.
• Create the container and define the objects your
application will depend on as services (e.g. aTwig
environment or database connection).
• Construct the $app with yonder container and get
dependencies as needed from it.
21. $container = new SlimContainer();
$container['twig'] = function($container) {
$loader = new
Twig_Loader_Filesystem('templates');
return new Twig_Environment($loader,
array('cache'));
};
$app = new SlimApp($container);