doz95923 2017-04-06 13:43
浏览 46

将参数从管道中间件传递到zend表达式2中的工厂类

I want to connect to different database according to URL. I try to set request attribute and get that attribute in *Factory.php.
I edit autoload/pipeline.php:

<?php
$app->pipe(UrlHelperMiddleware::class);
$app->pipe(\App\Action\Choose::class);
$app->pipeDispatchMiddleware();

in Choose.php I implement process() like this:

<?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);
}

The main problem is in *Factory.php I don't access to request.
Any try to access to Interop\Http\ServerMiddleware\MiddlewareInterface or Psr\Http\Message\ServerRequestInterface in *Factory.php raises same error.

Is there any way pass parameter from pipeline middleware to factory class?

  • 写回答

1条回答 默认 最新

  • dtpk04526211 2017-04-07 12:35
    关注

    If you use zend-servicemanager you can try this (just a theory, not tested):

    Create 2 database factories in your config: 'db.connection.a' => DbFactoryA::class, 'db.connection.b' => DbFactoryB::class,

    Then depending on the route, in Choose you load the connection you need and pass it to the container as the default connection. $db = $container->get('db.connection.a'); $container->setService('db.connection.default', $db);

    And now in all following middleware you can grab the default connection.

    UPDATE:

    As mentioned in the comments, this requires the container to be injected which is considered bad practice. How about you wrap the two in a common connection class and set the required from Choose:

    class Connection
    {
        private $connection;
    
        /**
         * @var ConnectionInterface
         */
        private $db_a;
    
        /**
         * @var ConnectionInterface
         */
        private $db_b;
    
        public function __construct(ConnectionInterface $a, ConnectionInterface $b)
        {
            $this->db_a = $a;
            $this->db_b = $b;
        }
    
        public function setConnection($connection)
        {
            if ($connection === 'a') {
                $this->connection = $this->db_a;
                return;
            }
    
            $this->connection = $this->db_b;
        }
    
        public function getConnection()
        {
            return $this->connection;
        }
    }
    

    Or you can inject only the config and create the database connection that's really needed. Store it in a property for caching (like the container does).

    评论

报告相同问题?

悬赏问题

  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测