+ All Categories
Home > Software > Middleware web APIs in PHP 7.x

Middleware web APIs in PHP 7.x

Date post: 22-Jan-2018
Category:
Upload: zend-by-rogue-wave-software
View: 84 times
Download: 1 times
Share this document with a friend
26
MIDDLEWARE WEB APIS IN PHP 7.X Jan Burkl Solution Consulting Manager , Dresden, 22nd September 2017 Rogue Wave Software php developer day 2017
Transcript
Page 1: Middleware web APIs in PHP 7.x

MIDDLEWAREWEBAPISINPHP7.XJanBurkl

SolutionConsultingManager

,Dresden,22ndSeptember2017

RogueWaveSoftware

phpdeveloperday2017

Page 2: Middleware web APIs in PHP 7.x
Page 3: Middleware web APIs in PHP 7.x

PHPPHP:HypertextPreprocessorThemostpopularserver-sidelanguage:PHPisusedby82.6%ofallthewebsites(source:

)UsedbyFacebook,Wikipedia,Yahoo,Etsy,Flickr,Digg,etc22yearsofusage,since1995FullOOPsupportsincePHP5

w3techs.com

Page 4: Middleware web APIs in PHP 7.x

PHP7Released:3December2015

Previousmajorwas ,13July2004

SkippedPHP6:Unicodefailure

Lastreleaseis (3Aug2017)

PHP5

7.1.8

Page 5: Middleware web APIs in PHP 7.x

PHP7PERFORMANCE

PHP7isalsofasterthan !Python3

Page 6: Middleware web APIs in PHP 7.x

BENCHMARK

PHP5.6 PHP7

MemoryUsage 428MB 33MB

Executiontime 0.49sec 0.06sec

$a=[];for($i=0;$i<1000000;$i++){$a[$i]=["hello"];}echomemory_get_usage(true);

Page 7: Middleware web APIs in PHP 7.x

MOVINGTOPHP7BadoosavedonemilliondollarsswitchingtoPHP7( )TumblrreducedthelatencyandCPUloadbyhalfmovingtoPHP7( )DailymotionhandlestwicemoretrafficwithsameinfrastructureswitchingtoPHP7( )

source

source

source

Page 8: Middleware web APIs in PHP 7.x

PHP7ISNOTONLYFAST!ReturnandScalarTypeDeclarationsImprovedExceptionhierarchyManyfatalerrorsconvertedtoExceptionsSecurerandomnumbergeneratorAuthenticatedencryptionAEAD(PHP7.1+)Nullabletypes(PHP7.1+)and !more

Page 9: Middleware web APIs in PHP 7.x

WEBAPISINPHP7

Page 10: Middleware web APIs in PHP 7.x

HTTPIN/OUT

Page 11: Middleware web APIs in PHP 7.x

EXAMPLERequest:

Response:

GET/api/version

HTTP/1.1200OKConnection:closeContent-Length:17Content-Type:application/json

{"version":"1.0"}

Page 12: Middleware web APIs in PHP 7.x

MIDDLEWAREAfunctionthatgetsarequestandgeneratesa

response

usePsr\Http\Message\ServerRequestInterfaceasRequest;useInterop\Http\ServerMiddleware\DelegateInterface;

function(Request$request,DelegateInterface$next){//doingsomethingwith$request...//forinstancecallingthedelegatemiddleware$next$response=$next->process($request);//manipulatethe$responsereturn$response;}

Page 13: Middleware web APIs in PHP 7.x

DELEGATEINTERFACE

DelegateInterfaceispartof HTTPMiddlewareproposal

namespaceInterop\Http\ServerMiddleware;

usePsr\Http\Message\ResponseInterface;usePsr\Http\Message\ServerRequestInterface;

interfaceDelegateInterface{/***@returnResponseInterface;*/publicfunctionprocess(ServerRequestInterface$request);}

PSR-15

Page 14: Middleware web APIs in PHP 7.x
Page 15: Middleware web APIs in PHP 7.x

EXPRESSIVE2.0ThePHPframeworkforMiddlewareapplications

PSR-7HTTPMessagesupport(using)

Supportoflambdamiddleware(PSR-15)anddoublepass($request,$response,$next)Pipingworkflow(using )Features:routing,dependencyinjection,templating,errorhandlingLastrelease2.0.3,28thMarch2017

zend-diactoros

zend-stratigility

Page 16: Middleware web APIs in PHP 7.x

INSTALLATIONYoucaninstallExpressive2.0using :

Choosethedefaultoptionsduringtheinstallation

composer

composercreate-project\zendframework/zend-expressive-skeletonapi

Page 17: Middleware web APIs in PHP 7.x

DEFAULTTheskeletonhas2URLasexample:/and

/api/ping

Theroutesareregisteredin/config/routes.php

Themiddlewareactionsarestoredin/src/App/Action

Page 18: Middleware web APIs in PHP 7.x

ROUTES

/config/routes.php

$app->get('/',App\Action\HomePageAction::class,'home');$app->get('/api/ping',App\Action\PingAction::class,'api.ping');

Page 19: Middleware web APIs in PHP 7.x

APIMIDDLEWARE

/src/App/Action/PingAction.php

namespaceApp\Action;

useInterop\Http\ServerMiddleware\DelegateInterface;useInterop\Http\ServerMiddleware\MiddlewareInterface;useZend\Diactoros\Response\JsonResponse;usePsr\Http\Message\ServerRequestInterface;

classPingActionimplementsMiddlewareInterface{publicfunctionprocess(ServerRequestInterface$request,DelegateInterface$delegate){returnnewJsonResponse(['ack'=>time()]);}}

Page 20: Middleware web APIs in PHP 7.x

PIPELINEWORKFLOW

/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);

Page 21: Middleware web APIs in PHP 7.x

SERVICECONTAINER

/config/container.php

useZend\ServiceManager\Config;useZend\ServiceManager\ServiceManager;

$config=require__DIR__.'/config.php';$container=newServiceManager();$config=newConfig($config['dependencies']);$config->configureServiceManager($container);$container->setService('config',$config);

return$container;

Page 22: Middleware web APIs in PHP 7.x

THEEXPRESSIVEAPP

/public/index.php

chdir(dirname(__DIR__));require'vendor/autoload.php';

call_user_func(function(){$container=require'config/container.php';$app=$container->get(\Zend\Expressive\Application::class);

require'config/pipeline.php';require'config/routes.php';

$app->run();});

Page 23: Middleware web APIs in PHP 7.x

ROUTEARESTAPI$app->route('/api/users[/{user-id}]',[Authentication\AuthenticationMiddleware::class,Authorization\AuthorizationMiddleware::class,Api\Action\UserAction::class],['GET','POST','PATCH','DELETE'],'api.users');

//orrouteeachHTTPmethod$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');

Page 24: Middleware web APIs in PHP 7.x

RESTDISPATCHTRAITusePsr\Http\Message\ServerRequestInterface;useInterop\Http\ServerMiddleware\DelegateInterface;

traitRestDispatchTrait{publicfunctionprocess(ServerRequestInterface$request,DelegateInterface$delegate){$method=strtolower($request->getMethod());if(method_exists($this,$method)){return$this->$method($request);}return$response->withStatus(501);//Methodnotimplemented}}

Page 25: Middleware web APIs in PHP 7.x

RESTMIDDLEWARE

Api\Action\UserAction.php

classUserActionimplementsMiddlewareInterface{useRestDispatchTrait;

publicfunctionget(ServerRequestInterface$request){$id=$request->getAttribute('user-id',false);$data=(false===$id)?/*allusers*/:/*userid*/;returnnewJsonResponse($data);}

publicfunctionpost(ServerRequestInterface$request){...}publicfunctionpatch(ServerRequestInterface$request){...}publicfunctiondelete(ServerRequestInterface$request){...}}

Page 26: Middleware web APIs in PHP 7.x

DANKESCHÖNSlides:http://5square.github.io/talks/

Moreinfo:

Contactme:jan.burkl[at]roguewave.com

Followme:

Credits:

Thisworkislicensedundera.

Iused tomakethispresentation.

https://framework.zend.com/blog

@janatzend

@ezimuel

CreativeCommonsAttribution-ShareAlike3.0UnportedLicensereveal.js


Recommended