Middleware in TYPO3

Middleware in TYPO3

Einsatz von Middleware in TYPO3 zum Beispiel für APIs oder Labels.

1e3f36cacc64d0a924cdbb3fdc59e8af?s=128

move:elevator

January 17, 2020
Tweet

Transcript

  1. None
  2. Middleware in TYPO3

  3. Daniel Möbius CMS-Developer

  4. Agenda Bisherige Möglichkeiten Middlware Live-Demo TypeNum eID PSR-7 PSR-15 Konfiguration

    Frontend-Entries Backend-Entries Service Zielstellung
  5. Zielstellung

  6. Zielstellung • Schnittstellen • Response-Manipulation • … Anforderungen

  7. Möglichkeiten

  8. Möglichkeiten abstract class AbstractApiController extends ActionController { /**@var \TYPO3\CMS\Extbase\Mvc\View\JsonView*/ protected

    $view; /**@var string*/ protected $resourceArgumentName; /**@var RepositoryInterface*/ protected $resourceRepository; /**@var Web\Request*/ protected $request; /** * AbstractApiController constructor. */ public function __construct() { parent::__construct(); $this->defaultViewObjectName = JsonView::class; } Page-Type
  9. Möglichkeiten protected function resolveActionMethodName() { parent::resolveActionMethodName(); $actionName = ‚'; switch

    ($this->request->getMethod()) { case 'POST': $actionName = 'create'; if( $this->request->hasArgument('method') && $this->request ->getArgument(‚method') === ‚edit' ){ $actionName= 'update'; } break; default: $this->throwStatus(400, null, 'Bad Request.'); } return $actionName . ‚Action'; } Page-Type
  10. Möglichkeiten api = PAGE api { typeNum = 9000 config

    { disableAllHeaderCode = 1 debug = 0 no_cache = 1 additionalHeaders { 10 { header = Content-Type: application/json replace = 1 } } } 10 < tt_content.list.20.dmtodos_tds } Page-Type
  11. Möglichkeiten $GLOBALS[‚TYPO3_CONF_VARS']['FE']['eID_include']['ServiceCenter'] = ‚EXT:service_center/Classes/Eid/ServiceCenterEid.php‘; eID

  12. Möglichkeiten <?php namespace Service\ServiceCenter\Eid; use TYPO3\CMS\Core\Database\DatabaseConnection; use TYPO3\CMS\Core\Utility\GeneralUtility; class ServiceCenterEid

    { public function main() { $resultSet = []; … return json_encode($resultSet); } } $eid = GeneralUtility::makeInstance(ServiceCenterEid::class); header('Content-Type: application/json'); echo $eid->main(); eID
  13. Middleware

  14. Middleware PSR-7 HTTP/1.1 200 OK Content-Type: text/plain This is the

    response body Psr\Http\Message\RequestInterface Psr\Http\Message\ResponseInterface $message = $message ->withHeader('foo', 'bar') ->withAddedHeader('foo', 'baz'); $header = $message->getHeaderLine('foo'); // $header contains: 'bar,baz' $header = $message->getHeader('foo'); // ['bar', 'baz']
  15. Middleware Middleware Middleware App Request Response PSR-15

  16. Middleware PSR-15 namespace Psr\Http\Server; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; /** *

    Participant in processing a server request and response. * * An HTTP middleware component participates in processing an HTTP message: * by acting on the request, generating the response, or forwarding the * request to a subsequent middleware and possibly acting on its response. */ interface MiddlewareInterface { /** * Process an incoming server request. * * Processes an incoming server request in order to produce a response. * If unable to produce the response itself, it may delegate to the provided * request handler to do so. */ public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface; }
  17. Middleware TYPO3-Konfiguration <?php return [ 'frontend' => [ 'identifier' =>

    [ 'target' => \Vendor\Extension\Middleware\Service::class, 'before' => [ 'typo3/cms-frontend/eid' ], ], ], 'backend' => [ 'identifier' => [ 'target' => \Vendor\Extension\Middleware\Service::class, 'after' => [ 'typo3/cms-backend/authentication' ] ], ], ];
  18. Middleware TYPO3-Konfiguration <?php return [ 'frontend' => [ 'identifier' =>

    [ 'disabled' => true ] ];
  19. Middleware Frontend-Entries

  20. Middleware Backend-Entries

  21. Middleware Service <?php declare(strict_types=1); namespace Vendor\Extension\Middleware; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface;

    use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; use TYPO3\CMS\Core\Http\HtmlResponse; class MiddlewareService implements MiddlewareInterface { public function process( ServerRequestInterface $request, RequestHandlerInterface $handler ): ResponseInterface { $response= $handler->handle($request); … return new HtmlResponse($content, $statusCode, $header); } }
  22. Live-Demo

  23. Fazit

  24. Danke! GitHub: https://github.com/danmoebius E-Mail: dm@move-elevator.de