How to inject Zend Service Manager in ZF3 Controllers
Zend Service Manager component is Zend Framework’s implementation of service locator pattern. This object is very usefull component for an application and is largely used in ZF applications.
Unfortunately in ZF3 default application, Service Manager component is no more available in controllers.
An official solution exsists for this, but in this little tutorial, I want to share an easy solution to inject Service Manager in all controllers.
To implement this solution, we will write our controllers by extending an abstract controller class written to handle a Service Manager instance. Then we will start to create controllers by invoking a factory class that inject Zend Service Manager instance into them.
Before start, let’s say that I will assume you just have a working Skeleton ZF3 Application and you just configured your ZF Service Manager.
First of all, we have to implement abstract controller class. This class will essentially have a setter and a getter for Service Manager object.
module/Application/src/Controller/AbstractController.php:
|
|
Now we need a factory class that will inject ServiceManager object in controllers.
module/Application/src/Controller/ControllerFactory.php:
|
|
Now we have all needed elements and we can implement our controller(s) extending AbstractController. Retreive of object from ServiceManager is shown at line 10.
module/Application/src/Controller/IndexController.php:
|
|
Finally to make everithing work, we have to instantiate controller in a bit different way.
module/Application/config/module.config.php:
|
|