简体   繁体   中英

How to remove “Zend\ServiceManager\Exception\ServiceNotFoundException” error in zend framework

We created a function in controller file and when we are running that function in the browser, we are getting

"Zend\\ServiceManager\\Exception\\ServiceNotFoundException" error. Our controller file code is

public function indexAction()
{


    if (! $this->getServiceLocator()
             ->get('AuthService')->hasIdentity()){
        return $this->redirect()->toRoute('login');
    }

    return new ViewModel();
}

Any idea about this error?

First of all, judging by the code you posted, this is Zend Framework 2 NOT Zend Framework 1.

You are getting this error because you are directly calling a service with the name AuthService , but that service doesn't exists. (There are plenty of reasons for this).

To solve your problem do this. First check to see if service exists.

//... more code

    if ($this->getServiceLocator()->has('AuthService')) {
        $auth = $this->getServiceLocator()->get('AuthService')->hasIdentity()
        if (!$auth) {
            return $this->redirect()->toRoute('login');
        }
        // code if user identity exists
    }

    return new ViewModel();
}

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