bnf / slim-typo3
Slim Framework集成到TYPO3
Requires
- bnf/slim3-psr15: ^1.0
- pimple/pimple: ^3.2
- psr/http-message: ^1.0
- psr/http-server-middleware: ^1.0
- slim/slim: ^3.0
- typo3/cms-core: ^9.5 || ~10.0.0
Requires (Dev)
- bnf/typo3-middleware: ~0.3.0
- php-coveralls/php-coveralls: ^2.0
- phpspec/prophecy: ^1.7
- typo3/testing-framework: ^2.0 || ^4.9 || ^5.0
Replaces
- slim_typo3: 0.5.0
- typo3-ter/slim_typo3: 0.5.0
README
简介
此扩展提供了一个TYPO3 RequestHandler,用于运行Slim App。当其中一个路由与请求匹配时,将执行Slim App。如果没有路由匹配,将执行优先级较低的默认TYPO3 RequestHandler。
此请求处理器基本上像TYPO3 eID一样工作(在相同的环境下执行),但具有合适的路由和美观的URL。
注意:EIDRequestHandler的优先级更高,不会受到此路由器的影响。这意味着slim应用不能接受GET参数eID
。
用法
$ composer require bnf/slim-typo3:~0.5.0
快速示例
在ext_localconf.php(或AdditionalConfiguration.php)中注册应用程序定义
\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; }); });
就这样,现在当请求/hello/world
时,应该执行您的路由控制器。
完整示例
- 待办事项:Configuration/Services.yaml – 基于标签的注册
ext_localconf.php
\TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\Bnf\SlimTypo3\AppRegistry::class) ->push(\Your\Namespace\TestApp::class);
TestApp.php
<?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; } }
示例扩展
有关示例扩展,请参阅https://github.com/bnf/slim-typo3-testapp。
待办事项
支持通过多个App配置生成多个RequestsHandlers?(也许还有basePaths)。(好吧,可能不是。缺点是这会在Slim\App之上添加另一个抽象层,而Slim\App已经知道关于路由组的信息 – 但它将支持不同的Middleware配置。)
示例
App1: /api
App2: /api2
App3: /getFooData.xml
建议
对于此TYPO3集成,我们建议将中间件作为字符串添加。这会有轻微的额外开销,因为Slim的CallableResolver
将使用preg_match匹配字符串,但优点是,并非所有中间件都需要实例化,即使它们可能永远不会被调用(因为没有任何路由匹配,我们也没有处理请求)。由于TYPO3是宿主,因此很可能大多数时候都会执行TYPO3 RequestHandlers,因此slim引导应该尽可能轻量。
您可以将中间件作为类(或容器标识符)字符串提供,Slim的CallableResolver
将从容器中检索实例(如果容器有该类的定义)或实例化一个新类:new $class($container);
因此使用
// 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');
而不是
// 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'));