SQLSTATE[08006] [7] 无法将主机名 "dbname=" 转换为地址

SQLSTATE[08006] [7] could not translate host name "dbname=" to address

我正在使用 Symfony/Silex SecurityServiceProvider 来登录网站上的用户。

http://silex.sensiolabs.org/doc/providers/security.html

我按照指南一步步操作,但是当我提交登录表单时,出现 PostgreSQL 错误

SQLSTATE[08006] [7] could not translate host name "dbname=" to address

无论我为 dbname 设置什么,我仍然得到错误。即使有远程服务器的正确 IP。但是我的设置很好,因为我使用相同的 class 来连接和请求服务器,而且它总是有效。

错误仅在提交登录表单时出现!

我的登录class

<?php

namespace Model;

use Silex\Application;

abstract class Entity {

    private $app;
    private $host;
    private $base;
    private $port;
    private $user;
    private $pass;
    private $charset;

    public function __construct(Application $app)
    {
        $this->app = $app;

        $this->host    = $app['config']['database']['host'];
        $this->base    = $app['config']['database']['base'];
        $this->port    = $app['config']['database']['port'];
        $this->user    = $app['config']['database']['user'];
        $this->pass    = $app['config']['database']['pass'];
        $this->charset = $app['config']['database']['charset'];
    }

    protected function connectBDD()
    {
        $connect = new \PDO("pgsql:host=$this->host;dbname=$this->base", $this->user, $this->pass);
        $connect->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
        $connect->query("SET NAMES '$this->charset'");

        return $connect;
    }
}
}

感谢帮助

您的配置值为空。 $app['config']['database']['host'] 为空,连接看起来像 $connect = new \PDO("pgsql:host=;dbname=", ...);
尝试转储配置以查看值

public function __construct(Application $app)
{
    dump($app['config']['database']);
    ...
}