我登录到一个登录页面,总是得到这个错误。我在代码中使用了Zend身份验证。这是我的登录页面控制器:
<?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的新手。请帮帮忙?
发布于 2015-12-21 19:19:08
看起来检索身份验证服务的配置密钥是'Zend\Authentication\AuthenticationService'而不是'AuthService'。试着使用
$this->authservice = $this->getServiceLocator()->get('Zend\Authentication\AuthenticationService');https://stackoverflow.com/questions/34393426
复制相似问题