Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend Framework 2 Override an existing Service?

I am using a zf2 module called GoalioRememberMe and now I want to override its service by my customized service. Or if it is not possible, I want to override the Module.php with my config. Is it possible?

In the Application module. I wrote this line in module.config.php:
'GoalioRememberMe\Service\RememberMe' => 'Application\Service\RememberMe'

Thanks in advance!

like image 911
Soroush Khosravi Avatar asked Nov 12 '13 12:11

Soroush Khosravi


2 Answers

This is exactly the reason it is recommended to name the service as the type of the object that is returned. The object GoalioRememberMe\Service\RememberMe is named goaliorememberme_rememberme_service in the service manager. You can check that here.

So the solution is simple, instead of this:

'GoalioRememberMe\Service\RememberMe' => 'Application\Service\RememberMe'

Write this

'goaliorememberme_rememberme_service' => 'Application\Service\RememberMe'
like image 163
Jurian Sluiman Avatar answered Jan 01 '23 19:01

Jurian Sluiman


As Jurian said, the service name is goaliorememberme_rememberme_service and it has been set in the getServiceConfig() method. So I wrote this code in the Module.php file in the Application Module:

$serviceManager->
            setAllowOverride(true)->
            setInvokableClass('goaliorememberme_rememberme_service', 'Application\Service\CustomRememberMe')->
            setAllowOverride(false);

And it replaced successfully with my customized service!
Thanks very much to Jurian for the big help!

like image 36
Soroush Khosravi Avatar answered Jan 01 '23 18:01

Soroush Khosravi