2015-12-21 42 views
0

I登錄到登錄頁面並始終出現此錯誤。我在我的代碼中使用了Zend認證。這裏是我的登錄頁面控制器:Zend ServiceManager ServiceManager :: get無法爲AuthService提取或創建實例

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

);

此外,我遵循類似的問題,但無法刪除該錯誤。

我曾嘗試對堆棧溢出這些問題看起來相似,但他們沒有幫助:

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新手。請幫忙?

回答

1

它看起來像檢索身份驗證服務的配置密鑰是'Zend\Authentication\AuthenticationService'而不是'AuthService'。嘗試使用

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

我試過了,仍然顯示相同的錯誤。 – Adi

+0

其實我也試過這個,也是一樣的, $ this-> authservice = $ this-> getServiceLocator() - > get('Zend \ Authentication \ Adapter \ AbstractAdapter'); – Adi

+0

我需要給予不同的路徑嗎?因爲我的身份驗證服務位於供應商內部> ZendFramework> ZendFramework>庫> Zend>身份驗證> AuthenticationService – Adi

相關問題