我登录登录页面并始终收到此错误。我在我的代码中使用了Zend Authentication。这是我的登录页面控制器:
<?php
namespace Webin\Controller;
use Zend\Session\Container;
use Zend\View\Model\ViewModel;
use Webin\Controller\AppController;
use Webin\Form\LoginForm;
use Webin\Form\Filter\LoginFilter;
use Webin\Utility\UserPassword;
class LoginController extends AppController {
protected $storage;
protected $authservice;
var $title="abc | ";
public function indexAction(){
$request = $this->getRequest();
$view = new ViewModel();
$view->title = $this->title."Login";
$loginForm = new LoginForm('loginForm');
$loginForm->setInputFilter(new LoginFilter() );
if($request->isPost()){
$data = $request->getPost();
$loginForm->setData($data);
if($loginForm->isValid()){
$data = $loginForm->getData();
$userPassword = new UserPassword('md5');
$encyptPass = $userPassword->create($data['password']);
$this->getAuthService()
->getAdapter()
->setIdentity($data['email'])
->setCredential($encyptPass);
$result = $this->getAuthService()->authenticate();
if ($result->isValid())
{
$session = new Container('User');
$session->offsetSet('email', $data['email']);
$this->flashMessenger()->addMessage(array('success' => 'Login Success.'));
// Redirect to page after successful login
}
else
{
$this->flashMessenger()->addMessage(array('error' => 'invalid credentials.'));
// Redirect to page after login failure
}
return $this->redirect()->tourl('/home');
// Logic for login authentication
}
else
{
$errors = $loginForm->getMessages();
//prx($errors);
}
}
$view->setVariable('loginForm', $loginForm);
return $view;
//return $this->redirect()->toUrl('http://www.webmobi.com/auth/signin')->setStatusCode(301);
}
private function getAuthService()
{
if (! $this->authservice) {
$this->authservice = $this->getServiceLocator()->get('AuthService');
//$this->authservice = $this->getServiceLocator()->get('Zend\Authentication\Adapter\AbstractAdapter');
//$db_array = $config['db'];
}
return $this->authservice;
}
public function logoutAction()
{
$session = new Container('User');
$session->getManager()->destroy();
$this->getAuthService()->clearIdentity();
return $this->redirect()->toUrl('/home');
}
}
?>
我的global.php看起来像这样:
return array(
'db' => array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=webmobi;host=localhost',
'driver_options' => array(
'PDO::MYSQL_ATTR_INIT_COMMAND' => 'SET NAMES \'UTF8\''
),
),
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter'
=> 'Zend\Db\Adapter\AdapterServiceFactory',
// 'Zend\Authentication\Adapter\AbstractAdapter'
// => 'Zend\Authentication\AuthenticationServiceInterface',
),
),
);
此外,我遵循了类似的问题,但无法删除该错误。
我已经尝试过关于堆栈溢出的这些问题,看起来很相似,但它们没有帮助。:
Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for db
我是ZF2的新手。请帮帮忙?