首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Zend\ServiceManager\ServiceManager::get无法获取或创建AuthService的实例

Zend\ServiceManager\ServiceManager::get无法获取或创建AuthService的实例
EN

Stack Overflow用户
提问于 2015-12-21 18:23:25
回答 1查看 1.6K关注 0票数 0

我登录到一个登录页面,总是得到这个错误。我在代码中使用了Zend身份验证。这是我的登录页面控制器:

代码语言:javascript
复制
    <?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看起来像这样:

代码语言:javascript
复制
    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',
    ),
),

);

此外,我遵循了类似的问题,但无法删除该错误。

我已经在堆栈溢出上尝试了这些问题,它们看起来很相似,但它们没有帮助:

zf2 navigation - 'Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for navigation'

please help me Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for Zend\Db\Adapter\Adapter

Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for db

我是ZF2的新手。请帮帮忙?

EN

回答 1

Stack Overflow用户

发布于 2015-12-21 19:19:08

看起来检索身份验证服务的配置密钥是'Zend\Authentication\AuthenticationService'而不是'AuthService'。试着使用

代码语言:javascript
复制
$this->authservice = $this->getServiceLocator()->get('Zend\Authentication\AuthenticationService');
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34393426

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档