< First How to print Sql query in ZendFramework2 >
Login.php namespace Movie\Form; use Zend\Form\Form; class Login extends Form { public function __construct() { parent::__construct('Movie'); $this->add(array('name'=>'email','type'=>'text', 'options'=>array('label'=>'Employee Email :'), 'attributes'=>array('id'=>'email'))); $this->add(array('name'=>'password','type'=>'password', 'options'=>array('label'=>'Employee Password :'), 'attributes'=>array('id'=>'password'))); $this->add(array('name'=>'submit','type'=>'submit', 'attributes'=>array('value'=>'Login','id'=>'btn'))); } } MoveController.php protected $authservice; private function getAuthService() { if (! $this->authservice) { $this->authservice = $this->getServiceLocator()->get('AuthService'); } return $this->authservice; } public function indexAction() { $login = new Login(); $request = $this->getRequest(); if($request->isPost()){ $data = $request->getPost(); $encyptPass = $data['password']; $this->getAuthService()->getAdapter() ->setIdentity($data['email']) ->setCredential($encyptPass); $result = $this->getAuthService()->authenticate(); if ($result->isValid()) { $this->redirect()->toRoute('movie', array('controller'=>'move','action'=>'listall')); }else{ $this->redirect()->toRoute('movie', array('controller'=>'move','action'=>'index')); } } return new ViewModel(array('login',$login)); } public function logoutAction() { $this->getAuthService()->clearIdentity(); return $this->redirect()->toRoute('login'); } If you are already login in, to redirect to success page if ($this->getAuthService()->hasIdentity()){ return $this->redirect()->toRoute('success') } If you want to check for login authentication if (! $this->getServiceLocator() ->get('AuthService')->hasIdentity()){ return $this->redirect()->toRoute('login'); } index.phtml <?php echo $this->form()->openTag($login); echo $this->formRow($login->get('email')); echo "<br>"; echo $this->formRow($login->get('password')); echo "<br>"; echo $this->formSubmit($login->get('submit')); echo $this->form()->closeTag(); ?> Module.php use Zend\Authentication\Adapter\DbTable as DbAuthAdapter; use Zend\Authentication\AuthenticationService; public function getServiceConfig() { return array( 'factories'=>array('Application\Model\StudentTable'=>function($sm){ $tableGateway=$sm->get('StudentTableGateway'); $table=new StudentTable($tableGateway); return $table; }, 'StudentTableGateway'=>function($sm){ $dbAdapter= $sm->get('Zend\Db\Adapter\Adapter'); $resultSetPrototype= new ResultSet(); $resultSetPrototype->setArrayObjectPrototype(new Student()); return new TableGateway('student',$dbAdapter,null,$resultSetPrototype ); }, 'AuthService' => function ($serviceManager) { $adapter = $serviceManager->get('Zend\Db\Adapter\Adapter'); $dbAuthAdapter = new DbAuthAdapter ( $adapter, 'users', 'email', 'password' ); $auth = new AuthenticationService(); $auth->setAdapter ( $dbAuthAdapter ); return $auth; } )); }