Slim Framework integration for TYPO3
, (*1)
Introduction
This extension provides a TYPO3 RequestHandler which runs a Slim App.
The Slim App will be executed when one of its routes match the request.
If no route matches, the lower prioritized default TYPO3
RequestHandler(s) will be executed., (*2)
This request handler basically works like a TYPO3 eID (executed with identically
environment), but with proper routing and nice looking URLs., (*3)
Note: The EIDRequestHandler has higher priority and will not
be influenced by this router. That means the slim app
can not accept a GET parameter eID
., (*4)
Usage
$ composer require bnf/slim-typo3:~0.5.0
Quick Example
Register the app definition in ext_localconf.php (or AdditionalConfiguration.php):, (*5)
\TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\Bnf\SlimTypo3\AppRegistry::class)
->push(function ($app) {
$app->get('/hello/{name}', function ($request, $response) {
$response->getBody()->write('Hello ' . htmlspecialchars($request->getAttribute('name')));
return $response;
});
});
That's all and now your route controller should be executed when requesting /hello/world
., (*6)
Full Example
- TODO: Configuration/Services.yaml – tag based registration
ext_localconf.php:, (*7)
\TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\Bnf\SlimTypo3\AppRegistry::class)
->push(\Your\Namespace\TestApp::class);
TestApp.php, (*8)
<?php
declare(strict_types=1);
namespace Your\Namespace;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\App;
use TYPO3\CMS\Core\SingletonInterface;
class TestApp implements SingletonInterface
{
public function __invoke(App $app)
{
$app->add(function (Request $request, Response $response, callable $next) {
$response->getBody()->write('I am middleware.. ');
return $next($request, $response, $next);
});
$app->get('/bar[/{foo}]', static::class . ':bar');
}
public function bar(Request $request, Response $response): Response
{
$response->getBody()->write('baz');
$foo = $request->getAttribute('foo');
if ($foo) {
$response->getBody()->write(' ' . htmlspecialchars($foo));
}
return $response;
}
}
Example Extension
See https://github.com/bnf/slim-typo3-testapp for an example extension., (*9)
TODO
Support generating multiple RequestsHandlers
with multiple App configs? (and maybe basePaths).
(Well, maybe not. Con: that add's another abstraction on top
of Slim\App which already knows about route groups – BUT
it would support different Middleware configs.), (*10)
Example:, (*11)
App1: /api
App2: /api2
App3: /getFooData.xml
Suggestions
For this TYPO3 integration we suggest to add middleware's as string.
That has a slight overhead as Slim's CallableResolver
will preg_match
the string, but has the advantage, that not all middleware's
need to be instantiated although they may never be called (as no route matches
and we do not process the request). As TYPO3 is the host
it's likely that most of the times the TYPO3 RequestHandlers will be executed
therefore the slim bootstrap should be as lightweight as possible., (*12)
You can supply middleware's as class (or container identifier) strings
and Slim's CallableResolver
will retrieve the instance
from the container (if the container has a definition for that class)
or instantiate a new class: new $class($container);
, (*13)
Therefore use:, (*14)
// Will implicitly call `new \Your\Namespace\Middleware\Foo($container)`
// when the middleware is executed.
$app->add(\Your\Namespace\Middleware\Foo::class);
// or ('view' needs to be defined in the container)
$app->add('view');
instead of:, (*15)
// You SHOULD NOT do this
$app->add(new \Your\Namespace\Middleware\Foo::class);
// nor this
$app->add($container->get(\Your\Namespace\Middleware\Foo::class));
// nor this
$app->add($container->get('view'));