Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Middleware in TYPO3

Middleware in TYPO3

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

move:elevator

January 17, 2020
Tweet

More Decks by move:elevator

Other Decks in Technology

Transcript

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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']
  6. 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; }
  7. 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' ] ], ], ];
  8. 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); } }