Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to Slim 3

Introduction to Slim 3

The Slim framework is a lightweight microframework that gives you the basic framework functionality without all the bloat. Now in version 3, the framework has undergone some dramatic changes since its previous version. Join me as I walk through some of its built-in features and show you how easy it is to work with and get a site up and running quickly.

I’ll also cover the creation of a simple REST API using the framework and how to include different modules for common web application related functionality.

Chris Cornutt

January 10, 2017
Tweet

More Decks by Chris Cornutt

Other Decks in Programming

Transcript

  1. Slim Framework Slim is a PHP micro framework that helps

    you quickly write simple yet powerful web applications and APIs. At its core, Slim is a dispatcher that receives an HTTP request, invokes an appropriate callback routine, and returns an HTTP response. That’s it. Simple Lightweight Easy to use & customize
  2. Slim Framework - Basic framework without the “bloat” - Not

    backwards compatible with Slim 2 - Easily extendible with middleware - Composer installable What’s included
  3. Slim Framework <?php use \Psr\Http\Message\ServerRequestInterface as Request; use \Psr\Http\Message\ResponseInterface as

    Response; require 'vendor/autoload.php'; $app = new \Slim\App; $app->get('/hello/{name}', function (Request $request, Response $response) { $name = $request->getAttribute('name'); $response->getBody()->write("Hello, $name"); return $response; }); $app->run();
  4. PSR-7: HTTP Messages http://www.php-fig.org/psr/psr-7/ - HTTP message abstraction - From

    the PHP-FIG standards group - RequestInterface & Response Interface - Streams - Headers, URI, Uploads, Messages
  5. Slim Framework <?php use \Psr\Http\Message\ServerRequestInterface as Request; use \Psr\Http\Message\ResponseInterface as

    Response; require 'vendor/autoload.php'; $app = new \Slim\App; $app->get('/hello/{name}', function (Request $request, Response $response) { $name = $request->getAttribute('name'); $response->getBody()->write("Hello, $name"); return $response; }); $app->run();
  6. Slim Framework: Config <?php $config = [ ‘settings’ => [

    ‘displayErrorDetails’ => true, ‘addContentLengthHeader’ => true, 'logger' => [ 'name' => 'slim-app', 'level' => Monolog\Logger::DEBUG, 'path' => __DIR__ . '/../logs/app.log', ] ] ]; $app = new \Slim\App($config); https://www.slimframework.com/docs/objects/application.html#application-configuration
  7. Slim Framework: Routes <?php $app = new \Slim\App(); $app->get(‘/‘, function($request,

    $response) { echo ‘GET home’; }); $app->post(‘/‘, function($request, $response) { echo ‘POST home’; }); $app->run(); GET, POST, PUT, DELETE, OPTIONS, PATCH
  8. Slim Framework: Routes $app->get(‘/hello/{name}’, function($request, $response, $args) { echo ‘GET

    home: ’.$args[‘name’]; }); $app->get(‘/id/{id:[0-9]+}’, function($request, $response, $args) { echo ‘GET id: ’.$args[‘id’]; }); $app->group(‘/user’, function() { $this->>get(‘/view’, function($request, $response) { echo ‘GET /user/view’; }); $this->post(‘/view’, function($request, $response) { echo ‘POST /user/view’; }); });
  9. Slim Framework: Routes class HomeController { public function home() {

    echo ‘GET /user/home’; } } $container = $app->getContainer(); $container[‘HomeController’] = function() { return new \HomeController(); }; $app->get(‘/user/home’, ‘\HomeController:home’);
  10. Slim Framework: DI <?php $config = [ ‘myobject’ => $obj1

    ]; $app = new \Slim\App($config); $app->get('/hello/{name}', function (Request $request, Response $response) { $object = $this->get(‘myobject’); }); $app->run(); https://www.slimframework.com/docs/objects/application.html#application-configuration Dependency injection
  11. Slim Framework: DI <?php $app = new \Slim\App(); $container =

    $app->getContainer(); $container[‘myclosure’] = function($container) { return (object)[‘foo’ => ‘bar’]; }; $app->get('/hello/{name}', function (Request $request, Response $response) { $object = $this->myclosure; }); $app->run(); https://www.slimframework.com/docs/objects/application.html#application-configuration Dependency injection
  12. Slim Framework: Request <?php $app = new \Slim\App(); $app->get('/hello/{name}', function

    ($request, $response) { echo $request->getMethod().’ on /hello/{name}’; if ($request->isGet()) { echo ‘getting!’; } $query = $request->getUri()-> getQuery(); echo ‘Accepting: ‘.$request->getHeader(‘Accept’); $body = $this->getBody(); // a \Psr\Http\Message\StreamInterface echo $body; return $response; }); $app->run();
  13. Slim Framework: Response <?php $app = new \Slim\App(); $app->get('/hello/{name}', function

    ($request, $response) { echo ‘HTTP status: ‘.$response->getStatusCode(); $newResponse = $response->withHeader(‘Content-Type’, ‘text/plain’); $body = $response->getBody(); $body->write(‘this is output back to the browser!’); // Adds “Content-Type: application/json" $newResponse = $response->withJson([‘foo’ => [‘bar’]); return $response; }); $app->run();
  14. Slim Framework: Errors <?php $app = new \Slim\App(); $container =

    $app->getContainer(); $container[‘errorHandler’] = function() { return function ($request, $response, $exception) use ($c) { return $c['response']->withStatus(500) ->withHeader('Content-Type', 'text/html') ->write('Something went wrong!'); }; }); $app->run();
  15. Slim Framework: Middleware <?php class ExampleMiddleware { public function __invoke($request,

    $response, $next) { $response->getBody()->write('BEFORE'); $response = $next($request, $response); $response->getBody()->write('AFTER'); return $response; } } $app = new \Slim\App(); $app->add(new \ExampleMiddleware()); $app->run();
  16. Slim Framework: Middleware <?php class ExampleMiddleware { public function __invoke($request,

    $response, $next) { $response->getBody()->write('BEFORE'); $response = $next($request, $response); $response->getBody()->write('AFTER'); return $response; } } $app = new \Slim\App(); $app->get(‘/‘, function($request, $response) { })->add(new \ExampleMiddleware()); $app->run();
  17. Slim Framework: Twig composer require slim/twig-view <?php $app = new

    \Slim\App(); $container = $app->getContainer(); $container[‘view’] = function() { return function ($request, $response, $exception) use ($c) { $view = new \Slim\Views\Twig(‘path/to/templates’, [ ‘cache’ => false ]); return $view; }); $app->run();
  18. Slim Framework: Twig <?php $app = new \Slim\App(); /* Setup

    from previous slide in container */ $app->get(‘/‘, function($request, $response) { $data = [ ‘foo’ => ‘bar’ ]; return $this->view->render($response, ‘template.php’, $data); }); $app->run();