SlideShare a Scribd company logo
HELLO WORLD ON
SLIM FRAMEWORK 3.X
Ryan Szrama (@ryanszrama)
CTO, Commerce Guys
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
composer require slim/slim:3.x-dev
<?php
require 'vendor/autoload.php';
$app = new SlimApp();
$app->get('/', function($request,
$response, $args) {
echo 'Hello, world!';
});
$app->run();
php -S localhost:8000
Hello World on Slim Framework 3.x
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.
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 SlimApp();
$app->get('/hello/{name:[A-Za-z]+}',
function($request, $response, $args) {
return $response->write('Hello, ' .
$args['name'] . '!');
})->setName('hello-name');
$app->run();
Hello World on Slim Framework 3.x
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.
Hello World on Slim Framework 3.x
$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 ';

});
Hello World on Slim Framework 3.x
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. aTwig
environment or database connection).
• Construct the $app with yonder container and get
dependencies as needed from it.
$container = new SlimContainer();
$container['twig'] = function($container) {
$loader = new
Twig_Loader_Filesystem('templates');
return new Twig_Environment($loader,
array('cache'));
};
$app = new SlimApp($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.

More Related Content

PDF
Bullet: The Functional PHP Micro-Framework
PDF
Keeping it Small: Getting to know the Slim Micro Framework
PDF
Keeping it small - Getting to know the Slim PHP micro framework
PPT
Slim RedBeanPHP and Knockout
ODP
Creating REST Applications with the Slim Micro-Framework by Vikram Vaswani
KEY
Silex, the microframework
ODP
Javascript laravel's friend
KEY
Keeping it small: Getting to know the Slim micro framework
Bullet: The Functional PHP Micro-Framework
Keeping it Small: Getting to know the Slim Micro Framework
Keeping it small - Getting to know the Slim PHP micro framework
Slim RedBeanPHP and Knockout
Creating REST Applications with the Slim Micro-Framework by Vikram Vaswani
Silex, the microframework
Javascript laravel's friend
Keeping it small: Getting to know the Slim micro framework

What's hot (20)

PPTX
Slim Framework
PDF
Bootstrat REST APIs with Laravel 5
PDF
AngularJS with Slim PHP Micro Framework
PDF
RESTful API development in Laravel 4 - Christopher Pecoraro
PDF
Inside Bokete: Web Application with Mojolicious and others
PDF
Developing apps using Perl
PPTX
Laravel Beginners Tutorial 1
PPTX
Laravel 5
PDF
Using the new WordPress REST API
PPT
Web service with Laravel
PDF
4.2 PHP Function
PDF
With a Mighty Hammer
PDF
Laravel Restful API and AngularJS
PDF
RESTful web services
PDF
Web services with laravel
PDF
Using Sinatra to Build REST APIs in Ruby
PDF
Building Cloud Castles
PDF
Extending the WordPress REST API - Josh Pollock
PPTX
Laravel for Web Artisans
PDF
深入淺出 MVC
Slim Framework
Bootstrat REST APIs with Laravel 5
AngularJS with Slim PHP Micro Framework
RESTful API development in Laravel 4 - Christopher Pecoraro
Inside Bokete: Web Application with Mojolicious and others
Developing apps using Perl
Laravel Beginners Tutorial 1
Laravel 5
Using the new WordPress REST API
Web service with Laravel
4.2 PHP Function
With a Mighty Hammer
Laravel Restful API and AngularJS
RESTful web services
Web services with laravel
Using Sinatra to Build REST APIs in Ruby
Building Cloud Castles
Extending the WordPress REST API - Josh Pollock
Laravel for Web Artisans
深入淺出 MVC
Ad

Similar to Hello World on Slim Framework 3.x (20)

PPTX
Slim 3 PHP micro framework
PDF
Micropage in microtime using microframework
PPTX
Introduction to Monsoon PHP framework
PDF
Durian: a PHP 5.5 microframework with generator-style middleware
PDF
An Introduction to the Laravel Framework (AFUP Forum PHP 2014)
PPTX
Introduction to Laravel Framework (5.2)
PPTX
PHP Frameworks, or how I learnt to stop worrying and love the code
PPTX
Ei cakephp
PPTX
Cakeph pppt
PPTX
What-is-Laravel-23-August-2017.pptx
PPTX
Laravel ppt
PDF
Slim PHP when you don't need the kitchen sink
PDF
Decouple your framework now, thank me later
PPTX
Lecture 2_ Intro to laravel.pptx
PDF
Mojolicious, real-time web framework
KEY
Future of PHP
PPTX
What-is-Laravel and introduciton to Laravel
PPTX
Day02 a pi.
PDF
Mojolicious
PDF
Intro to CakePHP
Slim 3 PHP micro framework
Micropage in microtime using microframework
Introduction to Monsoon PHP framework
Durian: a PHP 5.5 microframework with generator-style middleware
An Introduction to the Laravel Framework (AFUP Forum PHP 2014)
Introduction to Laravel Framework (5.2)
PHP Frameworks, or how I learnt to stop worrying and love the code
Ei cakephp
Cakeph pppt
What-is-Laravel-23-August-2017.pptx
Laravel ppt
Slim PHP when you don't need the kitchen sink
Decouple your framework now, thank me later
Lecture 2_ Intro to laravel.pptx
Mojolicious, real-time web framework
Future of PHP
What-is-Laravel and introduciton to Laravel
Day02 a pi.
Mojolicious
Intro to CakePHP
Ad

More from Ryan Szrama (7)

PDF
Making the Most of Modern PHP in Drupal 7
PDF
Developing with Configuration Management on Drupal 7
PPT
Drupal Commerce at DrupalCon Chicago
PPT
Paris Commerce Sprint
PPTX
Drupal Commerce, DrupalCamp Colorado 2010
PPTX
10 Tips for E-commerce on Drupal
PPTX
Drupal Commerce, Web Content 2010
Making the Most of Modern PHP in Drupal 7
Developing with Configuration Management on Drupal 7
Drupal Commerce at DrupalCon Chicago
Paris Commerce Sprint
Drupal Commerce, DrupalCamp Colorado 2010
10 Tips for E-commerce on Drupal
Drupal Commerce, Web Content 2010

Recently uploaded (20)

PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
System and Network Administraation Chapter 3
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PPTX
Online Work Permit System for Fast Permit Processing
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
System and Network Administration Chapter 2
PPTX
ai tools demonstartion for schools and inter college
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
AI in Product Development-omnex systems
PDF
top salesforce developer skills in 2025.pdf
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
medical staffing services at VALiNTRY
PDF
Nekopoi APK 2025 free lastest update
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PPTX
ManageIQ - Sprint 268 Review - Slide Deck
CHAPTER 2 - PM Management and IT Context
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
System and Network Administraation Chapter 3
Odoo Companies in India – Driving Business Transformation.pdf
VVF-Customer-Presentation2025-Ver1.9.pptx
Online Work Permit System for Fast Permit Processing
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
System and Network Administration Chapter 2
ai tools demonstartion for schools and inter college
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
AI in Product Development-omnex systems
top salesforce developer skills in 2025.pdf
Navsoft: AI-Powered Business Solutions & Custom Software Development
medical staffing services at VALiNTRY
Nekopoi APK 2025 free lastest update
Softaken Excel to vCard Converter Software.pdf
Wondershare Filmora 15 Crack With Activation Key [2025
ManageIQ - Sprint 268 Review - Slide Deck

Hello World on Slim Framework 3.x

  • 1. HELLO WORLD ON SLIM FRAMEWORK 3.X Ryan Szrama (@ryanszrama) CTO, Commerce Guys
  • 2. http://www.slimframework.com “Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs.”
  • 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
  • 7. <?php require 'vendor/autoload.php'; $app = new SlimApp(); $app->get('/', function($request, $response, $args) { echo 'Hello, world!'; }); $app->run();
  • 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.
  • 13. <?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();
  • 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.
  • 17. $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 ';
 });
  • 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);
  • 22. $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');
  • 23. http://ryanszrama.com/topics/slim Learning to develop with Slim 3.x helped me better understand essential concepts in Drupal 8.