SlideShare a Scribd company logo
MIDDLEWARE	WEB	APIS	IN	PHP	7.X
Jan	Burkl
Solution	Consulting	Manager
,	Dresden,	22nd	September	2017
Rogue	Wave	Software
php	developer	day	2017
Middleware web APIs in PHP 7.x
PHP
PHP:	Hypertext	Preprocessor
The	most	popular	server-side	language:	PHP	is
used	by	82.6%	of	all	the	websites	(source:
)
Used	by	Facebook,	Wikipedia,	Yahoo,	Etsy,
Flickr,	Digg,	etc
22	years	of	usage,	since	1995
Full	OOP	support	since	PHP	5
w3techs.com
PHP	7
Released:	3	December	2015
Previous	major	was	 ,	13	July	2004
Skipped	PHP	6:	Unicode	failure
Last	release	is	 	(3	Aug	2017)
PHP	5
7.1.8
PHP	7	PERFORMANCE
PHP	7	is	also	faster	than	 !Python	3
BENCHMARK
PHP	5.6 PHP	7
Memory	Usage 428	MB 33	MB
Execution	time 0.49	sec 0.06	sec
$a	=	[];
for	($i	=	0;	$i	<	1000000;	$i++)	{
		$a[$i]	=	["hello"];
}
echo	memory_get_usage(true);
MOVING	TO	PHP	7
Badoo	saved	one	million	dollars	switching	to	PHP
7	( )
Tumblr	reduced	the	latency	and	CPU	load	by	half
moving	to	PHP	7	( )
Dailymotion	handles	twice	more	traffic	with
same	infrastructure	switching	to	PHP	7	( )
source
source
source
PHP	7	IS	NOT	ONLY	FAST!
Return	and	Scalar	Type	Declarations
Improved	Exception	hierarchy
Many	fatal	errors	converted	to	Exceptions
Secure	random	number	generator
Authenticated	encryption	AEAD	(PHP	7.1+)
Nullable	types	(PHP	7.1+)
and	 !more
WEB	APIS	IN	PHP	7
HTTP	IN/OUT
EXAMPLE
Request:
Response:
GET	/api/version
HTTP/1.1	200	OK
Connection:	close
Content-Length:	17
Content-Type:	application/json
{
		"version":	"1.0"
}
MIDDLEWARE
A	function	that	gets	a	request	and	generates	a
response
use	PsrHttpMessageServerRequestInterface	as	Request;
use	InteropHttpServerMiddlewareDelegateInterface;
function	(Request	$request,	DelegateInterface	$next)
{
				//	doing	something	with	$request...
				//	for	instance	calling	the	delegate	middleware	$next
				$response	=	$next->process($request);
				//	manipulate	the	$response
				return	$response;
}
DELEGATEINTERFACE
DelegateInterface	is	part	of	 	HTTP	Middleware	proposal
namespace	InteropHttpServerMiddleware;
use	PsrHttpMessageResponseInterface;
use	PsrHttpMessageServerRequestInterface;
interface	DelegateInterface
{
				/**
					*	@return	ResponseInterface;
					*/
				public	function	process(ServerRequestInterface	$request);
}
PSR-15
Middleware web APIs in PHP 7.x
EXPRESSIVE	2.0
The	PHP	framework	for	Middleware	applications
PSR-7	HTTP	Message	support	(using	
)
Support	of	lambda	middleware	(PSR-15)	and
double	pass	($request,	$response,	$next)
Piping	workflow	(using	 )
Features:	routing,	dependency	injection,
templating,	error	handling
Last	release	2.0.3,	28th	March	2017
zend-
diactoros
zend-stratigility
INSTALLATION
You	can	install	Expressive	2.0	using	 :
Choose	the	default	options	during	the	installation
composer
composer	create-project	
		zendframework/zend-expressive-skeleton	api
DEFAULT
The	skeleton	has	2	URL	as	example:	/	and
/api/ping
The	routes	are	registered	in	/config/routes.php
The	middleware	actions	are	stored	in
/src/App/Action
ROUTES
/config/routes.php
$app->get('/',	AppActionHomePageAction::class,	'home');
$app->get('/api/ping',	AppActionPingAction::class,	'api.ping');
API	MIDDLEWARE
/src/App/Action/PingAction.php
namespace	AppAction;
use	InteropHttpServerMiddlewareDelegateInterface;
use	InteropHttpServerMiddlewareMiddlewareInterface;
use	ZendDiactorosResponseJsonResponse;
use	PsrHttpMessageServerRequestInterface;
class	PingAction	implements	MiddlewareInterface
{
				public	function	process(
								ServerRequestInterface	$request,
								DelegateInterface	$delegate
				)	{
								return	new	JsonResponse(['ack'	=>	time()]);
				}
}
PIPELINE	WORKFLOW
/config/pipeline.php
$app->pipe(ErrorHandler::class);
$app->pipe(ServerUrlMiddleware::class);
$app->pipeRoutingMiddleware();
$app->pipe(ImplicitHeadMiddleware::class);
$app->pipe(ImplicitOptionsMiddleware::class);
$app->pipe(UrlHelperMiddleware::class);
$app->pipeDispatchMiddleware();
$app->pipe(NotFoundHandler::class);
SERVICE	CONTAINER
/config/container.php
use	ZendServiceManagerConfig;
use	ZendServiceManagerServiceManager;
$config	=	require	__DIR__	.	'/config.php';
$container	=	new	ServiceManager();
$config	=	new	Config($config['dependencies']);
$config->configureServiceManager($container);
$container->setService('config',	$config);
return	$container;
THE	EXPRESSIVE	APP
/public/index.php
chdir(dirname(__DIR__));
require	'vendor/autoload.php';
call_user_func(function	()	{
				$container	=	require	'config/container.php';
				$app	=	$container->get(ZendExpressiveApplication::class
				require	'config/pipeline.php';
				require	'config/routes.php';
				$app->run();
});
ROUTE	A	REST	API
$app->route('/api/users[/{user-id}]',	[
				AuthenticationAuthenticationMiddleware::class,
				AuthorizationAuthorizationMiddleware::class,
				ApiActionUserAction::class
],	['GET',	'POST',	'PATCH',	'DELETE'],	'api.users');
//	or	route	each	HTTP	method
$app->get('/api/users[/{user-id}]',	...,	'api.users.get');
$app->post('/api/users',	...,	'api.users.post');
$app->patch('/api/users/{user-id}',	...,	'api.users.patch');
$app->delete('/api/users/{user-id}',	...,	'api.users.delete');
REST	DISPATCH	TRAIT
use	PsrHttpMessageServerRequestInterface;
use	InteropHttpServerMiddlewareDelegateInterface;
trait	RestDispatchTrait
{
				public	function	process(
								ServerRequestInterface	$request,
								DelegateInterface	$delegate
				)	{
								$method	=	strtolower($request->getMethod());
								if	(method_exists($this,	$method))	{
												return	$this->$method($request);
								}
								return	$response->withStatus(501);	//	Method	not	implem
				}
REST	MIDDLEWARE
ApiActionUserAction.php
class	UserAction	implements	MiddlewareInterface
{
				use	RestDispatchTrait;
				public	function	get(ServerRequestInterface	$request)
				{
								$id	=	$request->getAttribute('user-id',	false);
								$data	=	(false	===	$id)	?	/*	all	users	*/	:	/*	user	id	*/;
								return	new	JsonResponse($data);
				}
				public	function	post(ServerRequestInterface	$request){	...	}
				public	function	patch(ServerRequestInterface	$request){	...	}
				public	function	delete(ServerRequestInterface	$request){	...	
}
DANKE	SCHÖN
Slides:	http://5square.github.io/talks/
More	info:	
Contact	me:	jan.burkl	[at]	roguewave.com
Follow	me:	
Credits:	
This	work	is	licensed	under	a
.
I	used	 	to	make	this	presentation.
https://framework.zend.com/blog
@janatzend
@ezimuel
Creative	Commons	Attribution-ShareAlike	3.0	Unported	License
reveal.js

More Related Content

PDF
Developing web APIs using middleware in PHP 7
PDF
A Look at Command Line Swift
PDF
FISE Integration with Python and Plone
PDF
Big Fat FastPlone - Scale up, speed up
DOCX
Software Instructions
ODP
PDF
Building RT image with Yocto
Developing web APIs using middleware in PHP 7
A Look at Command Line Swift
FISE Integration with Python and Plone
Big Fat FastPlone - Scale up, speed up
Software Instructions
Building RT image with Yocto

What's hot (9)

PPT
The future of server side JavaScript
PDF
このPHP拡張がすごい!2017
PPTX
Build Python CMS The Plone Way
PDF
openPOWERLINK over Xenomai
PDF
Writing multi-language documentation using Sphinx
PDF
Ikazuchi introduction for Europython 2011 LT
PPS
Simplify your professional web development with symfony
PDF
Development and deployment with composer and kite
PDF
Using the "pip" package manager for Odoo/OpenERP - Opendays 2014
The future of server side JavaScript
このPHP拡張がすごい!2017
Build Python CMS The Plone Way
openPOWERLINK over Xenomai
Writing multi-language documentation using Sphinx
Ikazuchi introduction for Europython 2011 LT
Simplify your professional web development with symfony
Development and deployment with composer and kite
Using the "pip" package manager for Odoo/OpenERP - Opendays 2014
Ad

Similar to Middleware web APIs in PHP 7.x (20)

PDF
Develop web APIs in PHP using middleware with Expressive (Code Europe)
PDF
The new features of PHP 7
PDF
The new features of PHP 7 - Enrico Zimuel - Codemotion Milan 2016
PDF
PHP7 - For Its Best Performance
PDF
Start using PHP 7
PDF
Building web APIs in PHP with Zend Expressive
PDF
The why and how of moving to php 7
PPTX
Zeev Suraski "The PHP 7 Story, and beyond"
PPTX
Php 7 - YNS
ODP
The why and how of moving to php 7.x
ODP
The why and how of moving to php 7.x
PPTX
Rev it up with php on windows
PDF
PHP 7X New Features
PPT
PHP on Windows - What's New
PDF
PHP 7 performances from PHP 5
PDF
Php and webservices
PPTX
Php 5.6 vs Php 7 performance comparison
PPT
Wordpress On Windows
PPTX
Performance Comparison of PHP 5.6 vs. 7.0 vs HHVM
PDF
Php development and upcoming trends in 2017
Develop web APIs in PHP using middleware with Expressive (Code Europe)
The new features of PHP 7
The new features of PHP 7 - Enrico Zimuel - Codemotion Milan 2016
PHP7 - For Its Best Performance
Start using PHP 7
Building web APIs in PHP with Zend Expressive
The why and how of moving to php 7
Zeev Suraski "The PHP 7 Story, and beyond"
Php 7 - YNS
The why and how of moving to php 7.x
The why and how of moving to php 7.x
Rev it up with php on windows
PHP 7X New Features
PHP on Windows - What's New
PHP 7 performances from PHP 5
Php and webservices
Php 5.6 vs Php 7 performance comparison
Wordpress On Windows
Performance Comparison of PHP 5.6 vs. 7.0 vs HHVM
Php development and upcoming trends in 2017
Ad

More from Zend by Rogue Wave Software (20)

PDF
Develop microservices in php
PPTX
Speed and security for your PHP application
PPTX
Building and managing applications fast for IBM i
PPTX
To PHP 7 and beyond
PDF
Speed up web APIs with Expressive and Swoole (PHP Day 2018)
PDF
The Sodium crypto library of PHP 7.2 (PHP Day 2018)
PPTX
Ongoing management of your PHP 7 application
PDF
The Docker development template for PHP
PDF
The most exciting features of PHP 7.1
PPTX
Unit testing for project managers
PPTX
Deploying PHP apps on the cloud
PPTX
Data is dead. Long live data!
PPTX
Optimizing performance
PPTX
Resolving problems & high availability
PPTX
Developing apps faster
PPTX
Keeping up with PHP
PPTX
Fundamentals of performance tuning PHP on IBM i
PPTX
Getting started with PHP on IBM i
PDF
Continuous Delivery e-book
PDF
Standard CMS on standard PHP Stack - Drupal and Zend Server
Develop microservices in php
Speed and security for your PHP application
Building and managing applications fast for IBM i
To PHP 7 and beyond
Speed up web APIs with Expressive and Swoole (PHP Day 2018)
The Sodium crypto library of PHP 7.2 (PHP Day 2018)
Ongoing management of your PHP 7 application
The Docker development template for PHP
The most exciting features of PHP 7.1
Unit testing for project managers
Deploying PHP apps on the cloud
Data is dead. Long live data!
Optimizing performance
Resolving problems & high availability
Developing apps faster
Keeping up with PHP
Fundamentals of performance tuning PHP on IBM i
Getting started with PHP on IBM i
Continuous Delivery e-book
Standard CMS on standard PHP Stack - Drupal and Zend Server

Recently uploaded (20)

PPTX
ISO 45001 Occupational Health and Safety Management System
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PPTX
ai tools demonstartion for schools and inter college
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PPTX
L1 - Introduction to python Backend.pptx
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Nekopoi APK 2025 free lastest update
PDF
Understanding Forklifts - TECH EHS Solution
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
top salesforce developer skills in 2025.pdf
PPTX
Transform Your Business with a Software ERP System
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PPTX
Odoo POS Development Services by CandidRoot Solutions
ISO 45001 Occupational Health and Safety Management System
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
ai tools demonstartion for schools and inter college
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
How to Migrate SBCGlobal Email to Yahoo Easily
L1 - Introduction to python Backend.pptx
Softaken Excel to vCard Converter Software.pdf
Wondershare Filmora 15 Crack With Activation Key [2025
PTS Company Brochure 2025 (1).pdf.......
Nekopoi APK 2025 free lastest update
Understanding Forklifts - TECH EHS Solution
Navsoft: AI-Powered Business Solutions & Custom Software Development
VVF-Customer-Presentation2025-Ver1.9.pptx
Operating system designcfffgfgggggggvggggggggg
top salesforce developer skills in 2025.pdf
Transform Your Business with a Software ERP System
Upgrade and Innovation Strategies for SAP ERP Customers
Design an Analysis of Algorithms II-SECS-1021-03
Odoo POS Development Services by CandidRoot Solutions

Middleware web APIs in PHP 7.x