问题
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.
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',
),
),
);
This is my controller
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 :)
回答1:
What you may need is this -
$dbAdapter = $this->serviceLocator->get('Zend\Db\Adapter\Adapter');
If you need the exact db
array then do this -
$config = $this->serviceLocator->get('config');
$db_array = $config['db'];
I hope it helps.
来源:https://stackoverflow.com/questions/24625882/zend-servicemanager-servicemanagerget-was-unable-to-fetch-or-create-an-instanc