Hi all!

I'm writing my first ZF2 controller, and am wondering about accessing
other objects (models, services...) in a controller. As I see it now,
there are 2 ways to do this, and both ways seem perfectly fine to
me... But maybe I'm missing something I don't see, and in the long
run, one way is better than the other.

One way is by injecting the dependencies like this:
https://github.com/akrabat/zf2-tutorial/blob/master/module/Album/Module.php#L23
and then the controller has the setAlbumTable method, etc...

The other way is by configuring the service locator/manager/whatever
it's called, and then getting the object in the controller via
$this->getServiceLocator()->get('album-table') ...

Personally, I'm leaning towards the second way:
 - I know that the object is being called from the service locator
 - As long as the called object implements the
ServiceLocatorAwareInterface, it gets the service locator injected
(this maybe happens with the first way too, don't know, haven't
checked.)
 - From the unit tests PoV, it's... the same. Well, almost, the second
way requires a bit more configuring of the service locator in the
tests, but other than that, can't see any other difference. You'll
need to configure the SL for the first way too, if you want to access
the controller plugin manager/controller plugins.
 - My brain reads it/parses it/understands it easier.

So, is my brain completely wrong and the first way is better, or am I
correct going with the second option? Or maybe in the end it doesn't
matter, as it boils down to the same? OR, is it on a case-by-case
basis?

Thanks :)

-- 
~Robert Basic;
http://robertbasic.com/

-- 
List: fw-general@lists.zend.com
Info: http://framework.zend.com/archives
Unsubscribe: fw-general-unsubscr...@lists.zend.com


Reply via email to