简体   繁体   中英

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

I Logging into a login page and always getting this error. I have used Zend Authentication in my code . Here is my controller for login page :

    <?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'); 
                    }
    }
?>         

My global.php looks like this :

    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',
    ),
),

);

Furthermore I followed similar questions but unable to remove that error.

I have tried these questions on stack overflow which look similar but they didnt helped.:

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

I am newbie in ZF2 . Please Help?

It looks like that the configuration key to retrieve the authentication service is 'Zend\\Authentication\\AuthenticationService' and not 'AuthService' . Try to use

$this->authservice = $this->getServiceLocator()->get('Zend\Authentication\AuthenticationService');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM