by Enrico Zimuel
Senior Software Engineer
Rogue Wave Software, Inc.
ZendCon 2017, Las Vegas (NV), Oct. 25
|
interface DelegateInterface
{
public function process(
ServerRequestInterface $request
) : ResponseInterface;
}
PSR-15 ^0.4.1
interface MiddlewareInterface
{
public function process(
ServerRequestInterface $request,
DelegateInterface $delegate
) : ResponseInterface;
}
PSR-15 ^0.4.1
class FooMiddleware implements MiddlewareInterface
{
public function process(
ServerRequestInterface $request,
DelegateInterface $delegate
) {
$item = $this->repository->fetchById(
$request->getAttribute('id')
);
return new JsonResponse($item);
}
}
Install:
composer create-project zendframework/zend-expressive-skeleton <path-to-install>
Run (localhost:8008):
composer run --timeout=0 serve
use Zend\Log\Logger;
class LoginAction implements ServerMiddlewareInterface
{
public function __construct(Logger $logger, ...) {
$this->logger = $logger;
}
public function process(
ServerRequestInterface $request,
DelegateInterface $delegate
) {
// ...
$this->logger->log(Logger::INFO, "User $user logged in");
}
}
return [
'authentication' => [
'realm' => 'ZendCon2017',
'pdo' => [
'dsn' => 'sqlite:database.sq3',
'table' => 'user',
'field' => [
'username' => 'email',
'password' => 'pwd'
]
]
]
];
// config/routes.php
use Zend\Expressive\Authentication;
use Zend\Expressive\Helper;
$app->post('/api/admin/posts', [
Authentication\AuthenticationMiddleware::class,
Helper\BodyParams\BodyParamsMiddleware::class,
Book\Action\AddReviewAction::class
], 'admin.posts');
use Zend\Expressive\Authorization\AuthorizationInterface;
use Zend\Expressive\Authorization\Rbac\ZendRbac;
return [
'dependencies' => [
'aliases' => [
AuthorizationInterface::class => ZendRbac::class
]
]
];
return [
'authorization' => [
'roles' => [
'administrator' => [],
'user' => ['administrator']
],
'permissions' => [
'user' => [
'admin.dashboard',
'admin.posts'
],
'administrator' => [
'admin.settings',
],
]
]
];
// config/routes.php
use Zend\Expressive\Authentication;
use Zend\Expressive\Authorization;
use Zend\Expressive\Helper;
$app->post('/api/admin/posts', [
Authentication\AuthenticationMiddleware::class,
Authorization\AuthorizationMiddleware::class,
Helper\BodyParams\BodyParamsMiddleware::class,
Book\Action\AddReviewAction::class
], 'admin.posts');
use Zend\InputFilter\InputFilter;
class Posts extends InputFilter
{
public function init()
{
$this->add([
'name' => 'title',
'required' => true,
'filters' => [
['name' => StringTrim::class],
],
'validators' => [
[
'name' => StringLength::class,
'options' => ['min' => 1],
],
],
]);
}
}
class ContentValidationMiddleware implements MiddlewareInterface
{
public function __construct(InputFilter $filter)
{
$this->filter = $filter;
}
public function process(
ServerRequestInterface $request,
DelegateInterface $delegate
) {
$this->filter->setData($request->getParsedBody());
if (! $this->filter->isValid()) {
throw Exception\UnValidationException(
$this->filter->getMessages()
);
}
return $delegate->process(
$request->withParsedBody($this->filter->getValues())
);
}
}
Rate this talk at https://joind.in/talk/3c288