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