0

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?

Community
  • 1
  • 1
Adi
  • 43
  • 10

1 Answers1

1

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');
marcosh
  • 8,780
  • 5
  • 44
  • 74
  • In fact , I have tried it with this too , still same , $this->authservice = $this->getServiceLocator()->get('Zend\Authentication\Adapter\AbstractAdapter'); – Adi Dec 23 '15 at 06:39
  • Do I need to give diiferent path ? as my Authentication Service is inside vendor>ZendFramework>ZendFramework>library> Zend>Authentication>AuthenticationService – Adi Dec 23 '15 at 06:42
  • @Adi two questions: is your `AppController` extending `Zend\Mvc\Controller\AbstractController`? Are you using `zfc_user`? – marcosh Dec 23 '15 at 07:57
  • class AppController extends AbstractActionController { – Adi Dec 23 '15 at 08:01
  • basically appcontroller file is empty , just having a class defined , nothing else – Adi Dec 23 '15 at 08:02
  • @Adi I'm realising that the authentication service is not defined in the Service Manager by default, since you have to configure it with a Storage and an Adapter. You could try to have a look here to understand how to set these thins up: https://samsonasik.wordpress.com/2013/05/29/zend-framework-2-working-with-authenticationservice-and-db-session-save-handler/ – marcosh Dec 23 '15 at 09:09
  • That means do I have to make classes for AuthStorage and AuthStorageFactory ? . I mean I have a file named service manager in vendor folder which has numerous predefined functions. – Adi Dec 23 '15 at 11:17
  • I dont have a storage folder in my module , should I create new one? – Adi Dec 23 '15 at 11:20
  • @Adi you could implement your own AuthStorage or you can find some ready ones in Zend\Authentication\Storage. Storage does not refer to a folder, but to the persistence mechanism where you store the authentication details (i.e. username and passwords) of your users – marcosh Dec 23 '15 at 14:19
  • How to implement auth storage? . I mean I do not have any file in storage folder... – Adi Dec 24 '15 at 05:30
  • I have a model in my module , and it is the only model in whole application which stores username and password. should I make a new function for storage or a new folder in module.? – Adi Dec 24 '15 at 06:40