Download - Middleware web APIs in PHP 7.x
![Page 1: Middleware web APIs in PHP 7.x](https://reader034.vdocuments.site/reader034/viewer/2022051318/5a65bf3a7f8b9a38648b4ceb/html5/thumbnails/1.jpg)
MIDDLEWAREWEBAPISINPHP7.XJanBurkl
SolutionConsultingManager
,Dresden,22ndSeptember2017
RogueWaveSoftware
phpdeveloperday2017
![Page 2: Middleware web APIs in PHP 7.x](https://reader034.vdocuments.site/reader034/viewer/2022051318/5a65bf3a7f8b9a38648b4ceb/html5/thumbnails/2.jpg)
![Page 3: Middleware web APIs in PHP 7.x](https://reader034.vdocuments.site/reader034/viewer/2022051318/5a65bf3a7f8b9a38648b4ceb/html5/thumbnails/3.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022051318/5a65bf3a7f8b9a38648b4ceb/html5/thumbnails/4.jpg)
PHP7Released:3December2015
Previousmajorwas ,13July2004
SkippedPHP6:Unicodefailure
Lastreleaseis (3Aug2017)
PHP5
7.1.8
![Page 6: Middleware web APIs in PHP 7.x](https://reader034.vdocuments.site/reader034/viewer/2022051318/5a65bf3a7f8b9a38648b4ceb/html5/thumbnails/6.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022051318/5a65bf3a7f8b9a38648b4ceb/html5/thumbnails/7.jpg)
MOVINGTOPHP7BadoosavedonemilliondollarsswitchingtoPHP7( )TumblrreducedthelatencyandCPUloadbyhalfmovingtoPHP7( )DailymotionhandlestwicemoretrafficwithsameinfrastructureswitchingtoPHP7( )
source
source
source
![Page 8: Middleware web APIs in PHP 7.x](https://reader034.vdocuments.site/reader034/viewer/2022051318/5a65bf3a7f8b9a38648b4ceb/html5/thumbnails/8.jpg)
PHP7ISNOTONLYFAST!ReturnandScalarTypeDeclarationsImprovedExceptionhierarchyManyfatalerrorsconvertedtoExceptionsSecurerandomnumbergeneratorAuthenticatedencryptionAEAD(PHP7.1+)Nullabletypes(PHP7.1+)and !more
![Page 9: Middleware web APIs in PHP 7.x](https://reader034.vdocuments.site/reader034/viewer/2022051318/5a65bf3a7f8b9a38648b4ceb/html5/thumbnails/9.jpg)
WEBAPISINPHP7
![Page 10: Middleware web APIs in PHP 7.x](https://reader034.vdocuments.site/reader034/viewer/2022051318/5a65bf3a7f8b9a38648b4ceb/html5/thumbnails/10.jpg)
HTTPIN/OUT
![Page 11: Middleware web APIs in PHP 7.x](https://reader034.vdocuments.site/reader034/viewer/2022051318/5a65bf3a7f8b9a38648b4ceb/html5/thumbnails/11.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022051318/5a65bf3a7f8b9a38648b4ceb/html5/thumbnails/12.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022051318/5a65bf3a7f8b9a38648b4ceb/html5/thumbnails/13.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022051318/5a65bf3a7f8b9a38648b4ceb/html5/thumbnails/14.jpg)
![Page 15: Middleware web APIs in PHP 7.x](https://reader034.vdocuments.site/reader034/viewer/2022051318/5a65bf3a7f8b9a38648b4ceb/html5/thumbnails/15.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022051318/5a65bf3a7f8b9a38648b4ceb/html5/thumbnails/16.jpg)
INSTALLATIONYoucaninstallExpressive2.0using :
Choosethedefaultoptionsduringtheinstallation
composer
composercreate-project\zendframework/zend-expressive-skeletonapi
![Page 17: Middleware web APIs in PHP 7.x](https://reader034.vdocuments.site/reader034/viewer/2022051318/5a65bf3a7f8b9a38648b4ceb/html5/thumbnails/17.jpg)
DEFAULTTheskeletonhas2URLasexample:/and
/api/ping
Theroutesareregisteredin/config/routes.php
Themiddlewareactionsarestoredin/src/App/Action
![Page 18: Middleware web APIs in PHP 7.x](https://reader034.vdocuments.site/reader034/viewer/2022051318/5a65bf3a7f8b9a38648b4ceb/html5/thumbnails/18.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022051318/5a65bf3a7f8b9a38648b4ceb/html5/thumbnails/19.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022051318/5a65bf3a7f8b9a38648b4ceb/html5/thumbnails/20.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022051318/5a65bf3a7f8b9a38648b4ceb/html5/thumbnails/21.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022051318/5a65bf3a7f8b9a38648b4ceb/html5/thumbnails/22.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022051318/5a65bf3a7f8b9a38648b4ceb/html5/thumbnails/23.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022051318/5a65bf3a7f8b9a38648b4ceb/html5/thumbnails/24.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022051318/5a65bf3a7f8b9a38648b4ceb/html5/thumbnails/25.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022051318/5a65bf3a7f8b9a38648b4ceb/html5/thumbnails/26.jpg)
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