zendframework/zend-db

I couldn't get a login return shown me " login sucessful" but shown an exception message after runing LoginController.php

Closed this issue · 1 comments

  • I got an exception error as following:
    **An error occurred
    An error occurred during execution; please try again later.
    Additional information:
    Zend\ServiceManager\Exception\ServiceNotFoundException

File:

/var/www/gitest/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php:555

Message:

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

Stack trace:

#0 /var/www/gitest/module/Users/src/Users/Controller/LoginController.php(85): Zend\ServiceManager\ServiceManager->get('UserTable')
#1 /var/www/gitest/vendor/zendframework/zendframework/library/Zend/Mvc/Controller/AbstractActionController.php(82): Users\Controller\LoginController->processAction()
#2 [internal function]: Zend\Mvc\Controller\AbstractActionController->onDispatch(Object(Zend\Mvc\MvcEvent))
#3 /var/www/gitest/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(444): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#4 /var/www/gitest/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(205): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#5 /var/www/gitest/vendor/zendframework/zendframework/library/Zend/Mvc/Controller/AbstractController.php(118): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#6 /var/www/gitest/vendor/zendframework/zendframework/library/Zend/Mvc/DispatchListener.php(93): Zend\Mvc\Controller\AbstractController->dispatch(Object(Zend\Http\PhpEnvironment\Request), Object(Zend\Http\PhpEnvironment\Response))
#7 [internal function]: Zend\Mvc\DispatchListener->onDispatch(Object(Zend\Mvc\MvcEvent))
#8 /var/www/gitest/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(444): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#9 /var/www/gitest/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(205): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#10 /var/www/gitest/vendor/zendframework/zendframework/library/Zend/Mvc/Application.php(314): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#11 /var/www/gitest/public/index.php(17): Zend\Mvc\Application->run()
#12 {main}

Provide a narrative description of what you are trying to accomplish.
### I wish get some related good snippet codes for solving the issue and return a " Login Sucessfull "

Code to reproduce the issue

namespace Users\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

use Zend\Authentication\AuthenticationService;
use Zend\Authentication\Adapter\DbTable as DbTableAuthAdapter;

use Users\Form\LoginForm;
use Users\Form\LoginFilter;

use Users\Model\User;
use Users\Model\UserTable;



class LoginController extends AbstractActionController
{
    protected $storage;
    protected $authservice;
    
    public function getAuthService()
    {
        if (! $this->authservice) {
            $this->authservice = $this->getServiceLocator()->get('AuthService');
        }
        
        return $this->authservice;
    }
    
    public function logoutAction()
    {
        $this->getAuthService()->clearIdentity();
        
        return $this->redirect()->toRoute('users/login');
    }
    
    public function indexAction()
    {
        $form = $this->getServiceLocator()->get('LoginForm');
        $viewModel  = new ViewModel(array('form' => $form));
        return $viewModel;
    }
    
    public function processAction()
    {
        if (!$this->request->isPost()) {
            return $this->redirect()->toRoute('users/login');
        }
        
        $post = $this->request->getPost();
        
        $form = $this->getServiceLocator()->get('LoginForm');
        $userTable = $this->getServiceLocator()->get('UserTable');
        
        $form->setData($post);
        if (!$form->isValid()) {
            $model = new ViewModel(array(
                'error' => true,
                'form'  => $form,
            ));
            $model->setTemplate('users/login/index');
            return $model;
        } else {
            //check authentication...
            $this->getAuthService()->getAdapter()
            ->setIdentity($this->request->getPost('email'))
            ->setCredential($this->request->getPost('password'));
            $result = $this->getAuthService()->authenticate();
            if ($result->isValid()) {
                $this->getAuthService()->getStorage()->write($this->request->getPost('email'));
                return $this->redirect()->toRoute('users/login', array(
                    'action' =>  'confirm'
                ));
            } else {
                $model = new ViewModel(array(
                    'error' => true,
                    'form'  => $form,
                ));
                $model->setTemplate('users/login/index');
                return $model;
            }
        }
    }
    
    public function confirmAction()
    {
        $this->layout('layout/myaccount');
        $user_email = $this->getAuthService()->getStorage()->read();
        $viewModel  = new ViewModel(array(
            'user_email' => $user_email
        ));
        return $viewModel;
    }
    
}

Expected results

Actual results

@zhushaolei
I'm sorry this is the wrong place for your question. Please use the forum and add also your configuration which includes the definition for the UserTable class.