[ZF2] Best practice to get service in controller ?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

[ZF2] Best practice to get service in controller ?

MichaelB
Hi all!

I'm currently developing a web application with ZF2. I started some months ago and use $this->getServiceLocator() in controller to access to my service like described in the doc.

Example in the doc:
public function getAlbumTable()
     {
         if (!$this->albumTable) {
             $sm = $this->getServiceLocator();
             $this->albumTable = $sm->get('Album\Model\AlbumTable');
         }
         return $this->albumTable;
}


It work well! But i read on different discussion that it will may be removed in ZF3 and is better to inject all what you need in your controller directly in the constructor like this:


Example:
 public function __construct(FooServiceInterface $fooService, BarServiceInterface $barService)  
{  
        $this->fooService = $fooService;  
        $this->barService = $barService;  
}


So my question is easy.. What is the best practice ? (performance, migration, ...)


Some sources:
https://github.com/zendframework/zf2/issues/5168
http://macnibblet.blogspot.se/2013/10/why-i-wish-to-remove-getservicelocator.html


Thanks in advance,
Michael
Reply | Threaded
Open this post in threaded view
|

Re: [ZF2] Best practice to get service in controller ?

weierophinney
Administrator
On Sun, Dec 15, 2013 at 5:05 AM, MichaelB <[hidden email]> wrote:

> Hi all!
>
> I'm currently developing a web application with ZF2. I started some months
> ago and use $this->getServiceLocator() in controller to access to my service
> like described in the doc.
>
> /Example in the doc:/
>
>
>
> It work well! But i read on different discussion that it will may be removed
> in ZF3 and is better to inject all what you need in your controller directly
> in the constructor like this:
>
>
> /Example:/
>
>
>
> So my question is easy.. *What is the best practice ?* (performance,
> migration, ...)

Always inject your dependencies, and do not use and/or compose the
service manager instance in your controllers.

While it's nice for rapid prototyping, it leads to a number of issues:

- harder to determine the specific dependencies you have
- which in turn makes it harder to test your controllers

The appropriate way to handle dependencies with controllers is to
create factories for them and seed them to the ControllerManager. This
is done with either the "controllers" key in your configuration, or
the "getControllerConfig()" method of your Module classes. In each
case, the format follows that of all plugin managers and service
managers.

A best practice I use in my controller factories is to name the
incoming service locator instance so that I know what I'm pulling
from:

    function ($controllers) {
        $services = $controllers->getServiceLocator(); // the
application services
        return new MyController($services->get('MyDependency'));
    }


--
Matthew Weier O'Phinney
Project Lead            | [hidden email]
Zend Framework          | http://framework.zend.com/
PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: [ZF2] Best practice to get service in controller ?

MichaelB
Thanks Matthew for your answer!

Little other questions about it..

Is it a performance difference between creating a controller factory in a different class OR creating all in "getControllerConfig()" ?   (http://www.zfdaily.com/2012/07/getting-dependencies-into-zf2-controllers/)

And last question.. ;-) Form should be injected too ?


Thanks for your answer!
Michael
Reply | Threaded
Open this post in threaded view
|

Re: [ZF2] Best practice to get service in controller ?

weierophinney
Administrator
On Tue, Dec 17, 2013 at 12:57 PM, MichaelB <[hidden email]> wrote:
> Thanks Matthew for your answer!
>
> Little other questions about it..
>
> Is it a performance difference between creating a controller factory in a
> different class OR creating all in "getControllerConfig()" ?
> (http://www.zfdaily.com/2012/07/getting-dependencies-into-zf2-controllers/)

Putting it in a class actually is better, for a few reasons.

- First, if you never use the factory, no code is loaded. If you
define it in getControllerConfig(), you're defining a closure in each
and every request.
- Second, it gives you the option of extending or otherwise re-using
the factory later.

> And last question.. ;-) Form should be injected too ?

Typically, yes -- particularly as you can now seed the form elements,
hydrators, input filters and inputs, filters, and validators via
plugin managers, you'll likely want access to those plugin managers to
create your form -- so why not use a factory? :)


--
Matthew Weier O'Phinney
Project Lead            | [hidden email]
Zend Framework          | http://framework.zend.com/
PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: [ZF2] Best practice to get service in controller ?

localheinz
3. You can also test the factory.


Best regards,

Andreas
--
Andreas Möller
Web Developer

Twitter @localheinz

> On 17.12.2013, at 22:34, "Matthew Weier O'Phinney" <[hidden email]> wrote:
>
>> On Tue, Dec 17, 2013 at 12:57 PM, MichaelB <[hidden email]> wrote:
>> Thanks Matthew for your answer!
>>
>> Little other questions about it..
>>
>> Is it a performance difference between creating a controller factory in a
>> different class OR creating all in "getControllerConfig()" ?
>> (http://www.zfdaily.com/2012/07/getting-dependencies-into-zf2-controllers/)
>
> Putting it in a class actually is better, for a few reasons.
>
> - First, if you never use the factory, no code is loaded. If you
> define it in getControllerConfig(), you're defining a closure in each
> and every request.
> - Second, it gives you the option of extending or otherwise re-using
> the factory later.
>
>> And last question.. ;-) Form should be injected too ?
>
> Typically, yes -- particularly as you can now seed the form elements,
> hydrators, input filters and inputs, filters, and validators via
> plugin managers, you'll likely want access to those plugin managers to
> create your form -- so why not use a factory? :)
>
>
> --
> Matthew Weier O'Phinney
> Project Lead            | [hidden email]
> Zend Framework          | http://framework.zend.com/
> PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc
>
> --
> List: [hidden email]
> Info: http://framework.zend.com/archives
> Unsubscribe: [hidden email]
>
>

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]