Zend\ServiceManager\ServiceManager :: get无法获取或创建db实例 [英] Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for db
问题描述
我已登入表单。
我总是得到这个错误在我的控制器,它无法获取或创建一个这个数据库的实例。我已经读了所有的文档,为什么这个错误发生。我试过一切来解决这个问题,但它仍然不工作。有人可以帮助我。我使用我的数据库的其他事情,它可以进行连接,但当我想加载登录页面,我得到这个错误。
i have made a login form. I'm always getting this error in my controller that it was unable to fetch or create an instance for this db. i have read all the documentation about it and why this error occurs. i tried everything to fix this problem, but it is still not working. Can somebody help me. I'm using my database for other things and it can make a connection, but when i want to load the login page, i'm getting this error.
这是我的global.php中的代码
This is the code in my global.php
return array(
'db' => array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=music;host=localhost',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
),
'username' => 'root',
'password' => ''
),
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter'
=> 'Zend\Db\Adapter\AdapterServiceFactory',
),
),
);
这是我的控制器
public function indexAction()
{
/** @var Mysqli $dbAdapter */
$dbAdapter = $this->serviceLocator->get('db'); // This is where i'm getting the error from
$db = $this->params('fms');
$loginForm = new LoginForm();
if ($this->request->isPost()) {
$loginForm->setData($this->request->getPost());
if ($loginForm->isValid()) {
$auth = new AuthenticationService();
$result = $auth->authenticate(new SqlAdapter(
$loginForm->get('email')->getValue(),
$loginForm->get('password')->getValue(),
$dbAdapter
));
if ($result->getCode() == Result::SUCCESS) {
echo 'success';
return $this->redirect()->toUrl($this->url('')->fromRoute());
} else {
echo 'fail';
}
}
}
$this->view->loginForm = $loginForm;
return $this->view;
}
我不知道我在这里做错了。请有人帮助我:)
I don't know what i'm doing wrong here. Please somebody help me :)
推荐答案
您可能需要的是 -
What you may need is this -
$dbAdapter = $this->serviceLocator->get('Zend\Db\Adapter\Adapter');
如果您需要 db
array然后这样做 -
If you need the exact db
array then do this -
$config = $this->serviceLocator->get('config');
$db_array = $config['db'];
我希望它有帮助。
这篇关于Zend\ServiceManager\ServiceManager :: get无法获取或创建db实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!