I'm new to ZF2 and am having some trouble with what should be a basic idea.

I'm using the ZfCommons User module for authentication, it's been installed and is operating properly.

Now I want validate the user is in fact logged in from my controller based on this (How to check if the user is logged in but I can't figure out out how to register the controller plugin, I'm currently receiving this error:

Zend\Mvc\Controller\PluginManager::get was unable to fetch or create an instance for ZfcUserAuthentication

2 Answers
2

The exception is thrown because you are requesting the plugin in the constructor. Because of how plugins are tied to controllers, it is simply not possible to use plugins inside the constructor of the controller.

Background

The constructor is called first when an object is created. There is no other method called before __construct(). If you inject something in this new instance via a setter method, you therefore have no access to this in the constructor.

So using the most DRY method and putting the code in the module.php file - would I need to do $em->attach for every controller, or can I do it for all the controllers - in my app, everything needs to be protected so it'd be silly to have to attach the listener to every controller in the app.
–
R. DownJul 8 '13 at 3:02

Nevermind, another glance over the docs shows I can do it in the service manager.
–
R. DownJul 8 '13 at 3:09