SlideShare a Scribd company logo
PSR-7‫و‬middleware-‫ها‬‫معرفی‬ ‫و‬end Expressive
‫عربی‬ ‫میالد‬
8‫برنامه‬ ‫سال‬
‫نویس‬PHP
‫دهنده‬ ‫توسعه‬BSS/CRM
milad.arabi@gmail.com
@ooghry
Linkedin
microphp.org
I am a PHP developer
• I am not a Zend Framework or Symfony or CakePHP developer
• I think PHP is complicated enough
I like building small things
• I like building small things with simple purposes
• I like to make things that solve problems
• I like building small things that work together to solve larger problems
I want less code, not more
• I want to write less code, not more
• I want to manage less code, not more
• I want to support less code, not more
• I need to justify every piece of code I add to a project
I like simple, readable code
• I want to write code that is easily understood
• I want code that is easily verifiable
PSR-7 - Middleware - Zend Expressive
PSR-7 - Middleware - Zend Expressive
PSR-7 - Middleware - Zend Expressive
‫دنیای‬PHP‫سال‬ ‫در‬
2016
http://www.php-fig.org/members/
PHP Standards Recommendations
PSR-0(DEPRECATED) Autoloading
PSR-1 Basic coding standard
PSR-2 Coding style
PSR-3 Logger
PSR-4 Autoloading
PSR-5(DRAFT) PHPDoc Standard
PSR-6 Caching Interface
PSR-7 HTTP Message Interface
PSR-8(DRAFT) Huggable Interface
PSR-9(DRAFT) Security Advisories
<?php
namespace SymfonyComponentHttpFoundation;
class Request
{
public static function createFromGlobals()
{
$request = self::createRequestFromFactory(
$_GET,
$_POST,
array(),
$_COOKIE,
$_FILES,
$server
);
return $request;
}
}
<?php
namespace ZendHttpPhpEnvironment;
class Request extends HttpRequest
{
public function __construct()
{
$this->setEnv(new Parameters($_ENV));
if ($_GET) {
$this->setQuery(new Parameters($_GET));
}
if ($_POST) {
$this->setPost(new Parameters($_POST));
}
if ($_COOKIE) {
$this->setCookies(new Parameters($_COOKIE));
}
if ($_FILES) {
// convert PHP $_FILES superglobal
$files = $this->mapPhpFiles();
$this->setFiles(new Parameters($files));
}
$this->setServer(new Parameters($_SERVER));
}
}
PSR-7 - Middleware - Zend Expressive
HTTP Message Interface
• PsrHttpMessageMessageInterface
o PsrHttpMessageResponseInterface
o PsrHttpMessageRequestInterface
 PsrHttpMessageServerRequestInterface
• PsrHttpMessageStreamInterface
• PsrHttpMessageUploadFileInterface
• PsrHttpMessageUriInterface
Message
ResponseRequestStream
Uploaded File Server Request URI
extends
Wrapper
Local filesystem file://
HTTP Address http:// https://
FTP ftp:// ftps://
SSL ssl:// tls://
MySQL tcp://
Zip zip://
MongoDB mongodb://
PHP php://input php://output
php://temp php://memory
<?php
file_get_contents('local_file.json');
file_get_contents('http://site.com/file.json');
Middleware
http://stackphp.com/
https://zend-expressive.readthedocs.io/en/latest/getting-started/features/
function(
RequestInterface $request,
ResponseInterface $response,
callable $next=null
):ReponseInterface
<?php
namespace Mine;
class XClacksOverhead
{
public function __invoke($request, $response, $next)
{
$response = $next($request, $response);
return $response->withHeader(
'X-Clacks-Overhead',
'GNU Terry Pratchett'
);
}
}
Zend Framework
https://github.com/zendframework
zend-diactoros
https://github.com/zendframework/zend-diactoros
PSR-7 HTTP Message implementation
zend-stratigility
https://github.com/zendframework/zend-stratigility
Middleware for PHP built on top of PSR-7
Zend Expressive
•Routing interface
•CountainerInterface
•Templating Interfaceoptional
•Error handlingoptional
composer create-project zendframework/zend-expressive-skeleton
PSR-7 - Middleware - Zend Expressive
 ZendExpressiveRouterRouterInterface
• public function addRoute(Route $route);
• public function match(Request $request);
• public function generateUri($name, array $substitutions = []);
 InteropContainerContainerInterface
• public function get($id);
• public function has($id);
 ZendExpressiveTemplateTemplateRendererInterface
• public function render($name, $params = []);
• public function addPath($path, $namespace = null);
• public function getPaths();
• public function addDefaultParam($templateName, $param, $value);
 ZendStratigilityFinalHandler
PSR-7 - Middleware - Zend Expressive
PSR-7 - Middleware - Zend Expressive
.1‫از‬ ‫استفاده‬ ‫با‬ ‫نصب‬composer
.2‫تعریف‬VirtualHost
.3‫فایل‬ ‫ویرایش‬hosts‫ویندوز‬
PSR-7 - Middleware - Zend Expressive
Invokable Factory
<?php
class iClass
{
public function method1()
{
return time();
}
}
<?php
class fClass
{
private $adapter;
public function __construct($adapter)
{
$this->adapter=$adapter;
}
public function method1()
{
return $this
->adapter
->someMethod();
}
}
//--------------------------
class fClassFactory
{
public function __invoke()
{
$adapter=new NSAdapterClass;
return new fClass($adapter);
}
}
https://github.com/ooghry/Zend-Expressive-CoderConf
composer require zendframework/zend-db
config/autoload/db.global.php
config/autoload/db.local.php
config/autoload/routes.global.php
[
'name' => 'user.list',
'path' => '/list',
'middleware'=>AppActionListAction::class,
'allowed_methods' => ['GET'],
],
templates/app/list.html.twig
AppActionListAction
AppActionListActionFactory
<?php
namespace AppAction;
use InteropContainerContainerInterface;
use ZendDbAdapterAdapterInterface;
use ZendExpressiveTemplateTemplateRendererInterface;
class ListActionFactory
{
public function __invoke(ContainerInterface $container)
{
$template = ($container->has(TemplateRendererInterface::class))
? $container->get(TemplateRendererInterface::class)
: null;
$adapter = ($container->has(AdapterInterface::class))
? $container->get(AdapterInterface::class)
: null;
return new ListAction($template,$adapter);
}
}
<?php
namespace AppAction;
use ...
class ListAction
{
private $adapter;
private $template;
public function __construct(TemplateRendererInterface $template, $adapter)
{
$this->adapter = $adapter;
$this->template = $template;
}
public function __invoke(
ServerRequestInterface $request,
ResponseInterface $response,
callable $next = null
){
$statement = $this->adapter->query('select * from profile');
$users = $statement->execute();
return new HtmlResponse(
$this->template->render('app::list', ['users' => $users])
);
}
}
PSR-7 - Middleware - Zend Expressive
<?php
namespace AppAction;
use ZendDiactorosResponseJsonResponse;
use PsrHttpMessageResponseInterface;
use PsrHttpMessageServerRequestInterface;
class PingAction
{
public function __invoke(
ServerRequestInterface $request,
ResponseInterface $response,
callable $next = null)
{
return new JsonResponse(['ack' => time()]);
}
}
PSR-7 - Middleware - Zend Expressive
config/autoload/middleware-pipeline.global.php
'always' => [
'middleware' => [
HelperServerUrlMiddleware::class,
AppMiddlewareAuthMiddleware::class,
],
'priority' => PHP_INT_MAX,
],
config/autoload/dependencies.global.php
'factories' => [
AppMiddlewareAuthMiddleware::class => AppMiddlewareAuthMiddlewareFactory::class,
],
App
Auth
src/App/Middleware/AuthMiddleware.php
<?php
namespace AppMiddleware;
use ...
class AuthMiddleware
{
private $helper;
public function __construct(UrlHelper $helper)
{
$this->helper=$helper;
}
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
{
if($this->helper->generate('api.ping')!=$request->getUri()->getPath()) {
$auth = $this->parseAuth($request->getHeaderLine('Authorization'));
if (!$auth or !$this->checkUserPass($auth['user'], $auth['pass'])) {
return $response
->withHeader('WWW-Authenticate', 'Basic realm=""')
->withStatus(401);
}
}
$response = $next($request, $response);
return $response;
}
}
Download Middleware
config/autoload/routes.global.php
[
'name' => 'download',
'path' => '/download/{file:[_0-9a-zA-Z-]+.w{1,}}',
'middleware' => AppActionDownloadAction::class,
'allowed_methods' => ['GET'],
],
AppActionDownloadAction
class DownloadAction
{
public function __invoke($request, $response, $next = null)
{
$file='data'.$request->getUri()->getPath();
if(is_readable($file)){
return $response
->write(file_get_contents($file))
->withHeader(
'Content-Disposition',
'inline; filename="' . pathinfo($file,PATHINFO_BASENAME) . '"'
)
->withHeader('Content-type',pathinfo($file,PATHINFO_EXTENSION))
->withStatus(200);
}else{
return $next($request, $response);
}
}
}
PSR-7 - Middleware - Zend Expressive
App
Auth
Unavailable
config/autoload/middleware-pipeline.global.php
'middleware' => [
HelperServerUrlMiddleware::class,
AppMiddlewareUnavailableMiddleware::class,
AppMiddlewareAuthMiddleware::class,
],
AppMiddlewareUnavailableMiddleware
public function __invoke($request, $response, callable $next = null)
{
if (date('G') == '23' || (date('G') == '0' && date('i') < '30')) {
return new HtmlResponse($this->template->render('app::unavailable'));
}
$response = $next($request, $response);
return $response;
}
AppMiddlewareUnavailableMiddlewareFactory
templates/app/unavailable.html.twig
config/autoload/dependencies.global.php
PSR-7 - Middleware - Zend Expressive
‫متشکرم‬
PSR-7‫و‬middleware-‫معرفی‬ ‫و‬ ‫ها‬Zend Expressive
‫عربی‬ ‫میالد‬
milad.arabi@gmail.com
@ooghry
Linkedin
https://github.com/ooghry/Zend-Expressive-CoderConf

More Related Content

PDF
Zend expressive workshop
PDF
Expressive Microservice Framework Blastoff
PPTX
Zend Expressive in 15 Minutes
PDF
Foundations of Zend Framework
PDF
Virtualizing Development
PDF
Kicking off with Zend Expressive and Doctrine ORM (ZendCon 2016)
ODP
Deploying Perl apps on dotCloud
PDF
Php Dependency Management with Composer ZendCon 2016
Zend expressive workshop
Expressive Microservice Framework Blastoff
Zend Expressive in 15 Minutes
Foundations of Zend Framework
Virtualizing Development
Kicking off with Zend Expressive and Doctrine ORM (ZendCon 2016)
Deploying Perl apps on dotCloud
Php Dependency Management with Composer ZendCon 2016

What's hot (20)

PDF
PHP Development Tools
PDF
PHP7 - The New Engine for old good train
ODP
30 Minutes To CPAN
ODP
Getting started with Perl XS and Inline::C
PPTX
A brief to PHP 7.3
PPTX
Php internal architecture
PDF
Modern Web Development with Perl
PDF
Ninja Build: Simple Guide for Beginners
PDF
MobileConf 2021 Slides: Let's build macOS CLI Utilities using Swift
PDF
[Community Open Camp] 給 PHP 開發者的 VS Code 指南
PDF
Apigility – Lightning Fast API Development - OSSCamp 2014
PDF
Modern Black Mages Fighting in the Real World
PDF
Functional Programming for Busy Object Oriented Programmers
PPTX
PHP from soup to nuts Course Deck
PPT
Getting started with Catalyst and extjs
ODP
Practical catalyst
PDF
How to develop Jenkins plugin using to ruby and Jenkins.rb
ODP
Behat Workshop at WeLovePHP
KEY
Let's creating your own PHP (tejimaya version)
PDF
Building an API with Django and Django REST Framework
PHP Development Tools
PHP7 - The New Engine for old good train
30 Minutes To CPAN
Getting started with Perl XS and Inline::C
A brief to PHP 7.3
Php internal architecture
Modern Web Development with Perl
Ninja Build: Simple Guide for Beginners
MobileConf 2021 Slides: Let's build macOS CLI Utilities using Swift
[Community Open Camp] 給 PHP 開發者的 VS Code 指南
Apigility – Lightning Fast API Development - OSSCamp 2014
Modern Black Mages Fighting in the Real World
Functional Programming for Busy Object Oriented Programmers
PHP from soup to nuts Course Deck
Getting started with Catalyst and extjs
Practical catalyst
How to develop Jenkins plugin using to ruby and Jenkins.rb
Behat Workshop at WeLovePHP
Let's creating your own PHP (tejimaya version)
Building an API with Django and Django REST Framework
Ad

Viewers also liked (20)

PDF
20151030 富士通SS研 講演予定資料 @ 神戸
PPT
MEETLOAF
PDF
Creatividad e-innovación-en-tiempos-de-crisis-final
PDF
GSU MBA Leadership Speaker Series
PDF
Atlanta Women Entrepreneurs
PDF
すごい災害対応訓練2014浦安 事前説明
PDF
20110524 google earthの最前線
PDF
The 10 Unbreakable Laws Of Social Media
PDF
How2 openstreetmap gettingstarted
PPT
六合彩,香港六合彩 » SlideShare
PDF
Futebol
PDF
Popis usluga iz sustava javne nabave
PPT
Berninipp
PPTX
Leadership for Women Business Owners
PDF
20131019 青年土地家屋調査士 全国大会講演資料 2013
PPTX
NENV Design Studio 2014 F - 湖畔で学ベンチ
PDF
FOSS4G Tokyo 2011: THE UNIFORM PROJECT
PPT
12 17 08 Creating A Blog
PDF
Molome infrastructure
PPT
Library Media And Technology Services
20151030 富士通SS研 講演予定資料 @ 神戸
MEETLOAF
Creatividad e-innovación-en-tiempos-de-crisis-final
GSU MBA Leadership Speaker Series
Atlanta Women Entrepreneurs
すごい災害対応訓練2014浦安 事前説明
20110524 google earthの最前線
The 10 Unbreakable Laws Of Social Media
How2 openstreetmap gettingstarted
六合彩,香港六合彩 » SlideShare
Futebol
Popis usluga iz sustava javne nabave
Berninipp
Leadership for Women Business Owners
20131019 青年土地家屋調査士 全国大会講演資料 2013
NENV Design Studio 2014 F - 湖畔で学ベンチ
FOSS4G Tokyo 2011: THE UNIFORM PROJECT
12 17 08 Creating A Blog
Molome infrastructure
Library Media And Technology Services
Ad

Recently uploaded (20)

PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PPTX
L1 - Introduction to python Backend.pptx
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
Nekopoi APK 2025 free lastest update
PDF
Understanding Forklifts - TECH EHS Solution
PDF
Digital Strategies for Manufacturing Companies
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PPTX
Reimagine Home Health with the Power of Agentic AI​
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
top salesforce developer skills in 2025.pdf
PDF
medical staffing services at VALiNTRY
PPTX
history of c programming in notes for students .pptx
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Wondershare Filmora 15 Crack With Activation Key [2025
L1 - Introduction to python Backend.pptx
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
wealthsignaloriginal-com-DS-text-... (1).pdf
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Nekopoi APK 2025 free lastest update
Understanding Forklifts - TECH EHS Solution
Digital Strategies for Manufacturing Companies
VVF-Customer-Presentation2025-Ver1.9.pptx
Reimagine Home Health with the Power of Agentic AI​
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Design an Analysis of Algorithms I-SECS-1021-03
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
How to Choose the Right IT Partner for Your Business in Malaysia
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
top salesforce developer skills in 2025.pdf
medical staffing services at VALiNTRY
history of c programming in notes for students .pptx
Odoo Companies in India – Driving Business Transformation.pdf
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)

PSR-7 - Middleware - Zend Expressive