简体   繁体   English

Symfony 动态数据库凭证

[英]Symfony dynamic database credentials

Is it possible to make a input to set database user/password in Symfony at start-up of the application (like phpMyAdmin)?是否可以在应用程序启动时输入以在 Symfony 中设置数据库用户/密码(如 phpMyAdmin)?

I want that my application do like this:我希望我的应用程序这样做:

  • Welcome page (only html, no database connection with just input for db username/password (like phpMyAdmin))欢迎页面(仅 html,没有数据库连接,只需输入 db 用户名/密码(如 phpMyAdmin))
  • User set username/password on inputs and click ok button => the values can be set into Request/post or Session in the controller by example用户在输入上设置用户名/密码并单击确定按钮 => 值可以设置为请求/发布或 controller 中的 Session 示例
  • Symfony connect database to all application and redirect user to 'app_index' route Symfony 将数据库连接到所有应用程序并将用户重定向到“app_index”路由

Is it possible to do it with Symfony 5?是否可以使用 Symfony 5 来做到这一点?

Here my controller action:这是我的 controller 动作:

public function database_login(Request $request, Connection $connection): Response
  {
    if ($request->isMethod('POST') && $request->request->has('dbpass')) {
      $dbpass = $request->request->get('dbpass');
      $params = $connection->getParams();
      $params['password'] = $dbpass;

      $connection->__construct($params, $connection->getDriver(), $connection->getConfiguration(), $connection->getEventManager());
      $connection->connect();
      return $this->redirectToRoute('app_index');
    }
    return new Response(
      '<form method="post">
        <input type="password" name="dbpass">
        <button type="submit">OK</button>
      </form>'
    );
  }

The new connection work's fine but it lost after redirecToRoute() function.新的连接工作正常,但在redirecToRoute() function 后丢失。

Can you help me?你能帮助我吗?

@barbuslex, you can save variables to the session. @barbuslex,您可以将变量保存到 session。

$request->getSession()->set('db_login', 'login');
$request->getSession()->set('db_password', 'password');

Also, to avoid passing these parameters manually every request, you could use EventListener, for example: https://symfony.com/doc/current/event_dispatcher.html#request-events-checking-types此外,为避免在每个请求中手动传递这些参数,您可以使用 EventListener,例如: https://symfony.com/doc/current/event_dispatcher.html#request-events-checking-types

However, maybe you need another event, that is triggered earlier.但是,也许您需要另一个更早触发的事件。 Or play around with "priority" of listener.或者玩弄听众的“优先级”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM