0
votes

I'm new to Zend Framework 3. And migrating my Zend Framework 2 application to Zend Framework 3.

Currently I'm trying to call userPlugin from Application Module's Module.php file.

Here is my code,

public function onRoute(MvcEvent $e)
{
    $sm = $e->getApplication()->getServiceManager();
    $userPlugin = $sm->get('ControllerPluginManager')->get('userPlugin');
    $userPlugin->setServiceManager($e->getApplication()->getServiceManager());
    $checkLogin = $userPlugin->isLoggedIn();
}

And in my module.config.php,

'controller_plugins' => array(
    //This is also not working
    //'invokables' => array(
    //    'userPlugin' => 'User\Controller\Plugin\UserPlugin',
    //),
    'factories' => [
        Controller\Plugin\UserPlugin::class => InvokableFactory::class,
    ],
    'aliases' => [
        'userPlugin' => Controller\Plugin\UserPlugin::class,
    ]
)

But getting this error

Uncaught Zend\ServiceManager\Exception\ServiceNotFoundException: A plugin by the name "UserPlugin" was not found in the plugin manager Zend\Mvc\Controller\PluginManager

Where am I wrong?

1
ZF3 might be a bit more strict about your aliases. I'm not sure at all! You registered it without a capital letter, while your module.php is calling: "UserPlugin". It would be nicer to use the FQCN as in: ->get(\MyModule\Controller\Plugin\UserPlugin::class) instead of ->get('UserPlugin'). Could you check if that is working?Kwido
@Kwido I have tried with you solution also. But not worked for me.Keyur
Can you show Module.php class or how do you attach to route event? Also, does your plugin work in controller?SzymonM
The error message and code relating to the ->get('userPlugin') do not match up. "UserPlugin" (in error message) is not "userPlugin" (as shown in your example). @Kwido is correct, In ZF3 this will cause you an issue as the service names are literal strings. Are you sure you are using ->get('userPugin') and not ->get('UserPlugin');.AlexP

1 Answers

3
votes

Use below code

 $sm = $e->getApplication()->getServiceManager();
 $checkLogin = $sm->get('ControllerPluginManager')
                  ->get(Controller\Plugin\UserPlugin::class)
                  ->isLoggedIn();