Upload
ryan-szrama
View
586
Download
0
Tags:
Embed Size (px)
Citation preview
http://www.slimframework.com
“Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs.”
Created by Josh Lockhart (a.k.a. @codeguy),author of Modern PHP and PHP the Right Way.
http://www.phptherightway.com
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
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
<?php
require 'vendor/autoload.php';
$app = new \Slim\App();
$app->get('/', function($request, $response, $args) { echo 'Hello, world!'; });
$app->run();
GROKKING THE 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.
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');
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.
<?php
require 'vendor/autoload.php';
$app = new \Slim\App();
$app->get('/hello/{name:[A-Za-z]+}', function($request, $response, $args) {
return $response->write('Hello, ' . $args['name'] . '!');
})->setName('hello-name');
$app->run();
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.
$app->add(function ($request, $response, $next) {
$response->write('BEFORE'); $response = $next($request, $response); $response->write('AFTER');
return $response; });
$app->get('/', function ($request, $response, $args) {
echo ' Hello ';
});
LAYERING MIDDLEWARE• Use middleware to manage user sessions and
authentication, content negotiation, caching, etc.
(From http://stackphp.com.)
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. a Twig environment or database connection).
• Construct the $app with yonder container and get dependencies as needed from it.
$container = new \Slim\Container();
$container['twig'] = function($container) {
$loader = new Twig_Loader_Filesystem('templates');
return new Twig_Environment($loader, array('cache'));
};
$app = new \Slim\App($container);
$app->get('/hello', function($request, $response, $args) {
// Remember, $this is the $app. $template = $this->getContainer()-> get(‘twig')->loadTemplate('index.html');
return $response->write($template-> render(['content' => 'Hello, world!’]));
})->setName('hello-world');
http://ryanszrama.com/topics/slim
Learning to develop with Slim 3.x helped me better understand essential concepts in Drupal 8.