Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Hello World on Slim Framework 3.x


Published on

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.

Published in: Software

Hello World on Slim Framework 3.x

  1. 1. HELLO WORLD ON SLIM FRAMEWORK 3.X Ryan Szrama (@ryanszrama) CTO, Commerce Guys
  2. 2. “Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs.”
  3. 3. Created by Josh Lockhart (a.k.a. @codeguy), author of Modern PHP and PHP the Right Way.
  4. 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. 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
  6. 6. composer require slim/slim:3.x-dev
  7. 7. <?php require 'vendor/autoload.php'; $app = new SlimApp(); $app->get('/', function($request, $response, $args) { echo 'Hello, world!'; }); $app->run();
  8. 8. php -S localhost:8000
  9. 9. 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.
  10. 10. 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');
  11. 11. 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.
  12. 12. <?php require 'vendor/autoload.php'; $app = new SlimApp(); $app->get('/hello/{name:[A-Za-z]+}', function($request, $response, $args) { return $response->write('Hello, ' . $args['name'] . '!'); })->setName('hello-name'); $app->run();
  13. 13. 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.
  14. 14. $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 ';
  15. 15. LAYERING MIDDLEWARE • Use middleware to manage user sessions and authentication, content negotiation, caching, etc. (From
  16. 16. 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.
  17. 17. $container = new SlimContainer(); $container['twig'] = function($container) { $loader = new Twig_Loader_Filesystem('templates'); return new Twig_Environment($loader, array('cache')); }; $app = new SlimApp($container);
  18. 18. $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');
  19. 19. Learning to develop with Slim 3.x helped me better understand essential concepts in Drupal 8.