我想根据 URL 连接到不同的数据库。我尝试设置请求属性并在*Factory.php
.
我编辑autoload/pipeline.php
:
<?php
$app->pipe(UrlHelperMiddleware::class);
$app->pipe(\App\Action\Choose::class);
$app->pipeDispatchMiddleware();
在Choose.php
我process()
这样实现:
<?php
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
{
/** @var RouteResult $route */
$route = $request->getAttribute(RouteResult::class);
if ($route->getMatchedRouteName() == 'FirstRoute' or $route->getMatchedRouteName() == 'SecondRoute') {
$request = $request->withAttribute('DB_NAME', $route->getMatchedParams()['param']);
}
return $delegate->process($request);
}
主要问题是*Factory.php
我无法访问请求。
任何访问Interop\Http\ServerMiddleware\MiddlewareInterface
或访问Psr\Http\Message\ServerRequestInterface
*Factory.php 的尝试都会引发相同的错误。
有没有办法将参数从管道中间件传递到工厂类?