0
votes

Is it possible to make a input to set database user/password in Symfony at start-up of the application (like 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))
  • 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
  • Symfony connect database to all application and redirect user to 'app_index' route

Is it possible to do it with Symfony 5?

Here my controller action:

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.

Can you help me ?

1
HTTP is stateless. If you do not keep the DB login details somewhere, obviously whatever credentials the user entered are lostyivi
Yes, i want to use Session or $config->setParameter instead of Request but i don't know how to use it to inject into doctrine for all controllers. The documentation dont explain how to use it :Sbarbuslex
why don't you create login page and secure all routes with access_control unless /login ? like this all application will be secured and you'll be redirected to login automaticallyhous
i want make the user hability to connect to database with his own db credentials (like phpmyadmin)barbuslex

1 Answers

0
votes

@barbuslex, you can save variables to the 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

However, maybe you need another event, that is triggered earlier. Or play around with "priority" of listener.