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
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();
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. 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

What's hot

Slim Framework
Slim FrameworkSlim Framework
Slim Framework
Pramod Raghav
 
Bootstrat REST APIs with Laravel 5
Bootstrat REST APIs with Laravel 5Bootstrat REST APIs with Laravel 5
Bootstrat REST APIs with Laravel 5
Elena Kolevska
 
AngularJS with Slim PHP Micro Framework
AngularJS with Slim PHP Micro FrameworkAngularJS with Slim PHP Micro Framework
AngularJS with Slim PHP Micro Framework
Backand Cohen
 
RESTful API development in Laravel 4 - Christopher Pecoraro
RESTful API development in Laravel 4 - Christopher PecoraroRESTful API development in Laravel 4 - Christopher Pecoraro
RESTful API development in Laravel 4 - Christopher Pecoraro
Christopher Pecoraro
 
Inside Bokete: Web Application with Mojolicious and others
Inside Bokete:  Web Application with Mojolicious and othersInside Bokete:  Web Application with Mojolicious and others
Inside Bokete: Web Application with Mojolicious and others
Yusuke Wada
 
Developing apps using Perl
Developing apps using PerlDeveloping apps using Perl
Developing apps using Perl
Anatoly Sharifulin
 
Laravel Beginners Tutorial 1
Laravel Beginners Tutorial 1Laravel Beginners Tutorial 1
Laravel Beginners Tutorial 1
Vikas Chauhan
 
Laravel 5
Laravel 5Laravel 5
Laravel 5
Sudip Simkhada
 
Using the new WordPress REST API
Using the new WordPress REST APIUsing the new WordPress REST API
Using the new WordPress REST API
Caldera Labs
 
Web service with Laravel
Web service with LaravelWeb service with Laravel
Web service with Laravel
Abuzer Firdousi
 
4.2 PHP Function
4.2 PHP Function4.2 PHP Function
4.2 PHP Function
Jalpesh Vasa
 
With a Mighty Hammer
With a Mighty HammerWith a Mighty Hammer
With a Mighty Hammer
Ben Scofield
 
Laravel Restful API and AngularJS
Laravel Restful API and AngularJSLaravel Restful API and AngularJS
Laravel Restful API and AngularJSBlake Newman
 
Web services with laravel
Web services with laravelWeb services with laravel
Web services with laravel
Confiz
 
Using Sinatra to Build REST APIs in Ruby
Using Sinatra to Build REST APIs in RubyUsing Sinatra to Build REST APIs in Ruby
Using Sinatra to Build REST APIs in Ruby
LaunchAny
 
Building Cloud Castles
Building Cloud CastlesBuilding Cloud Castles
Building Cloud Castles
Ben Scofield
 
Extending the WordPress REST API - Josh Pollock
Extending the WordPress REST API - Josh PollockExtending the WordPress REST API - Josh Pollock
Extending the WordPress REST API - Josh Pollock
Caldera Labs
 
Laravel for Web Artisans
Laravel for Web ArtisansLaravel for Web Artisans
Laravel for Web Artisans
Raf Kewl
 
深入淺出 MVC
深入淺出 MVC深入淺出 MVC
深入淺出 MVCJace Ju
 

What's hot (20)

Slim Framework
Slim FrameworkSlim Framework
Slim Framework
 
Bootstrat REST APIs with Laravel 5
Bootstrat REST APIs with Laravel 5Bootstrat REST APIs with Laravel 5
Bootstrat REST APIs with Laravel 5
 
AngularJS with Slim PHP Micro Framework
AngularJS with Slim PHP Micro FrameworkAngularJS with Slim PHP Micro Framework
AngularJS with Slim PHP Micro Framework
 
RESTful API development in Laravel 4 - Christopher Pecoraro
RESTful API development in Laravel 4 - Christopher PecoraroRESTful API development in Laravel 4 - Christopher Pecoraro
RESTful API development in Laravel 4 - Christopher Pecoraro
 
Inside Bokete: Web Application with Mojolicious and others
Inside Bokete:  Web Application with Mojolicious and othersInside Bokete:  Web Application with Mojolicious and others
Inside Bokete: Web Application with Mojolicious and others
 
Developing apps using Perl
Developing apps using PerlDeveloping apps using Perl
Developing apps using Perl
 
Laravel Beginners Tutorial 1
Laravel Beginners Tutorial 1Laravel Beginners Tutorial 1
Laravel Beginners Tutorial 1
 
Laravel 5
Laravel 5Laravel 5
Laravel 5
 
Using the new WordPress REST API
Using the new WordPress REST APIUsing the new WordPress REST API
Using the new WordPress REST API
 
Web service with Laravel
Web service with LaravelWeb service with Laravel
Web service with Laravel
 
4.2 PHP Function
4.2 PHP Function4.2 PHP Function
4.2 PHP Function
 
With a Mighty Hammer
With a Mighty HammerWith a Mighty Hammer
With a Mighty Hammer
 
Laravel Restful API and AngularJS
Laravel Restful API and AngularJSLaravel Restful API and AngularJS
Laravel Restful API and AngularJS
 
RESTful web services
RESTful web servicesRESTful web services
RESTful web services
 
Web services with laravel
Web services with laravelWeb services with laravel
Web services with laravel
 
Using Sinatra to Build REST APIs in Ruby
Using Sinatra to Build REST APIs in RubyUsing Sinatra to Build REST APIs in Ruby
Using Sinatra to Build REST APIs in Ruby
 
Building Cloud Castles
Building Cloud CastlesBuilding Cloud Castles
Building Cloud Castles
 
Extending the WordPress REST API - Josh Pollock
Extending the WordPress REST API - Josh PollockExtending the WordPress REST API - Josh Pollock
Extending the WordPress REST API - Josh Pollock
 
Laravel for Web Artisans
Laravel for Web ArtisansLaravel for Web Artisans
Laravel for Web Artisans
 
深入淺出 MVC
深入淺出 MVC深入淺出 MVC
深入淺出 MVC
 

Similar to Hello World on Slim Framework 3.x

MERN SRTACK WEB DEVELOPMENT NODE JS EXPRESS
MERN SRTACK WEB DEVELOPMENT NODE JS EXPRESSMERN SRTACK WEB DEVELOPMENT NODE JS EXPRESS
MERN SRTACK WEB DEVELOPMENT NODE JS EXPRESS
annalakshmi35
 
23003468463PPT.pptx
23003468463PPT.pptx23003468463PPT.pptx
23003468463PPT.pptx
annalakshmi35
 
High quality ap is with api platform
High quality ap is with api platformHigh quality ap is with api platform
High quality ap is with api platform
Nelson Kopliku
 
REST API Best Practices & Implementing in Codeigniter
REST API Best Practices & Implementing in CodeigniterREST API Best Practices & Implementing in Codeigniter
REST API Best Practices & Implementing in Codeigniter
Sachin G Kulkarni
 
REST-API introduction for developers
REST-API introduction for developersREST-API introduction for developers
REST-API introduction for developers
Patrick Savalle
 
Kicking off with Zend Expressive and Doctrine ORM (ConFoo YVR 2017)
Kicking off with Zend Expressive and Doctrine ORM (ConFoo YVR 2017)Kicking off with Zend Expressive and Doctrine ORM (ConFoo YVR 2017)
Kicking off with Zend Expressive and Doctrine ORM (ConFoo YVR 2017)
James Titcumb
 
Remedie: Building a desktop app with HTTP::Engine, SQLite and jQuery
Remedie: Building a desktop app with HTTP::Engine, SQLite and jQueryRemedie: Building a desktop app with HTTP::Engine, SQLite and jQuery
Remedie: Building a desktop app with HTTP::Engine, SQLite and jQueryTatsuhiko Miyagawa
 
Kicking off with Zend Expressive and Doctrine ORM (PHP South Africa 2018)
Kicking off with Zend Expressive and Doctrine ORM (PHP South Africa 2018)Kicking off with Zend Expressive and Doctrine ORM (PHP South Africa 2018)
Kicking off with Zend Expressive and Doctrine ORM (PHP South Africa 2018)
James Titcumb
 
WordCamp Raleigh 2016 - WP API, What is it good for? Absolutely Everything!
WordCamp Raleigh 2016 - WP API, What is it good for? Absolutely Everything!WordCamp Raleigh 2016 - WP API, What is it good for? Absolutely Everything!
WordCamp Raleigh 2016 - WP API, What is it good for? Absolutely Everything!
Evan Mullins
 
Overview of RESTful web services
Overview of RESTful web servicesOverview of RESTful web services
Overview of RESTful web services
nbuddharaju
 
Super simple introduction to REST-APIs (2nd version)
Super simple introduction to REST-APIs (2nd version)Super simple introduction to REST-APIs (2nd version)
Super simple introduction to REST-APIs (2nd version)
Patrick Savalle
 
Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vliss...
Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vliss...Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vliss...
Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vliss...King Foo
 
Where is my scalable api?
Where is my scalable api?Where is my scalable api?
Where is my scalable api?
Altoros
 
Where is my scalable API?
Where is my scalable API?Where is my scalable API?
Where is my scalable API?
Juan Pablo Genovese
 
ExpressJS-Introduction.pdf
ExpressJS-Introduction.pdfExpressJS-Introduction.pdf
ExpressJS-Introduction.pdf
Bareen Shaikh
 
An Introduction to Websphere sMash for PHP Programmers
An Introduction to Websphere sMash for PHP ProgrammersAn Introduction to Websphere sMash for PHP Programmers
An Introduction to Websphere sMash for PHP Programmers
jphl
 
Kicking off with Zend Expressive and Doctrine ORM (PHPNW2016)
Kicking off with Zend Expressive and Doctrine ORM (PHPNW2016)Kicking off with Zend Expressive and Doctrine ORM (PHPNW2016)
Kicking off with Zend Expressive and Doctrine ORM (PHPNW2016)
James Titcumb
 
What's New In Laravel 5
What's New In Laravel 5What's New In Laravel 5
What's New In Laravel 5
Darren Craig
 

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

Intro to Rack
Intro to RackIntro to Rack
Intro to Rack
 
MERN SRTACK WEB DEVELOPMENT NODE JS EXPRESS
MERN SRTACK WEB DEVELOPMENT NODE JS EXPRESSMERN SRTACK WEB DEVELOPMENT NODE JS EXPRESS
MERN SRTACK WEB DEVELOPMENT NODE JS EXPRESS
 
23003468463PPT.pptx
23003468463PPT.pptx23003468463PPT.pptx
23003468463PPT.pptx
 
High quality ap is with api platform
High quality ap is with api platformHigh quality ap is with api platform
High quality ap is with api platform
 
REST API Best Practices & Implementing in Codeigniter
REST API Best Practices & Implementing in CodeigniterREST API Best Practices & Implementing in Codeigniter
REST API Best Practices & Implementing in Codeigniter
 
REST-API introduction for developers
REST-API introduction for developersREST-API introduction for developers
REST-API introduction for developers
 
Kicking off with Zend Expressive and Doctrine ORM (ConFoo YVR 2017)
Kicking off with Zend Expressive and Doctrine ORM (ConFoo YVR 2017)Kicking off with Zend Expressive and Doctrine ORM (ConFoo YVR 2017)
Kicking off with Zend Expressive and Doctrine ORM (ConFoo YVR 2017)
 
Remedie: Building a desktop app with HTTP::Engine, SQLite and jQuery
Remedie: Building a desktop app with HTTP::Engine, SQLite and jQueryRemedie: Building a desktop app with HTTP::Engine, SQLite and jQuery
Remedie: Building a desktop app with HTTP::Engine, SQLite and jQuery
 
Kicking off with Zend Expressive and Doctrine ORM (PHP South Africa 2018)
Kicking off with Zend Expressive and Doctrine ORM (PHP South Africa 2018)Kicking off with Zend Expressive and Doctrine ORM (PHP South Africa 2018)
Kicking off with Zend Expressive and Doctrine ORM (PHP South Africa 2018)
 
WordCamp Raleigh 2016 - WP API, What is it good for? Absolutely Everything!
WordCamp Raleigh 2016 - WP API, What is it good for? Absolutely Everything!WordCamp Raleigh 2016 - WP API, What is it good for? Absolutely Everything!
WordCamp Raleigh 2016 - WP API, What is it good for? Absolutely Everything!
 
Overview of RESTful web services
Overview of RESTful web servicesOverview of RESTful web services
Overview of RESTful web services
 
Super simple introduction to REST-APIs (2nd version)
Super simple introduction to REST-APIs (2nd version)Super simple introduction to REST-APIs (2nd version)
Super simple introduction to REST-APIs (2nd version)
 
Rest with Spring
Rest with SpringRest with Spring
Rest with Spring
 
Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vliss...
Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vliss...Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vliss...
Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vliss...
 
Where is my scalable api?
Where is my scalable api?Where is my scalable api?
Where is my scalable api?
 
Where is my scalable API?
Where is my scalable API?Where is my scalable API?
Where is my scalable API?
 
ExpressJS-Introduction.pdf
ExpressJS-Introduction.pdfExpressJS-Introduction.pdf
ExpressJS-Introduction.pdf
 
An Introduction to Websphere sMash for PHP Programmers
An Introduction to Websphere sMash for PHP ProgrammersAn Introduction to Websphere sMash for PHP Programmers
An Introduction to Websphere sMash for PHP Programmers
 
Kicking off with Zend Expressive and Doctrine ORM (PHPNW2016)
Kicking off with Zend Expressive and Doctrine ORM (PHPNW2016)Kicking off with Zend Expressive and Doctrine ORM (PHPNW2016)
Kicking off with Zend Expressive and Doctrine ORM (PHPNW2016)
 
What's New In Laravel 5
What's New In Laravel 5What's New In Laravel 5
What's New In Laravel 5
 

More from Ryan Szrama

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

More from Ryan Szrama (7)

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

Recently uploaded

In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
Juraj Vysvader
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
Ortus Solutions, Corp
 
Enterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptxEnterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptx
QuickwayInfoSystems3
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
Globus
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Globus
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
abdulrafaychaudhry
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
rickgrimesss22
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
Philip Schwarz
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
Fermin Galan
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
informapgpstrackings
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
e20449
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Shahin Sheidaei
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
Matt Welsh
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Globus
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
Cyanic lab
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Globus
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
AMB-Review
 
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket ManagementUtilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate
 

Recently uploaded (20)

In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
 
Enterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptxEnterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptx
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
 
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket ManagementUtilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
 

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();
  • 9.
  • 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();
  • 14.
  • 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.
  • 16.
  • 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 ';
 });
  • 18.
  • 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.