I can't for the life of me get $this->getServiceLocator() to work in my controller. I've read and tried everything. I'm guessing I'm missing something?? Here is some code.
namespace Login\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\Session\Container as SessionContainer;
use Zend\Session\SessionManager;
use Zend\View\Model\ViewModel;
use Zend\Mvc\Controller;
use Login\Model\UserInfo;
class LoginController extends AbstractActionController
{
private $db;
public function __construct()
{
$sm = $this->getServiceLocator();
$this->db = $sm->get('db');
}
...
The error I'm getting is:
Fatal error: Call to a member function get() on a non-object in /product/WishList/module/Login/src/Login/Controller/LoginController.php on line 21
I think it is besause your Controller isn't implement the interface of ServiceLocatorAwareInterface.You can see the class Zend\ServiceManager\AbstractPluginManager,in the construct method:
so you must implement it if you want use the ServiceLocator,or extend the class Zend\Mvc\Controller\AbstractActionController it have implementd the ServiceLocatorAwareInterface.
To give my comment a little bit more meaning. The ServiceLocator (or rather all ControllerPlugins) are only available at a later point of the livecycle of the Controller. If you wish you assign a variable that you can easily use throughout your actions, i suggest to either use Lazy-Getters or to inject them using the Factory Pattern
Lazy-Getters
Factory-Pattern
Hope that's understandable ;)