Hi Ralph! I'd say this is a good practice! The view should not know that there is a pizza service, but the helper can have that. In fact, many helpers have references to more complex services like the router or generally things that are not view-specific.
Marco Pivetta http://twitter.com/Ocramius http://ocramius.github.com/ On 26 January 2013 10:03, Ralf Eggert <[email protected]> wrote: > Hi, > > in a discussion an issue came up regarding good or bad practice for ZF2 > view helpers. > > Is it good or bad practice to inject other objects, especially model > services, into a view helper to call methods to get some data fetched? > > For example within the __invoke() method I could get the data from a > service like this: > > ------------------------------------------------------------------------ > namespace Pizza\View\Helper; > > use Zend\Form\View\Helper\AbstractHelper; > use Pizza\Service\PizzaService; > > class RandomPizza extends AbstractHelper > { > protected $service = null; > > public function __construct(PizzaService $service) > { > $this->setPizzaService($service); > } > > public function setPizzaService(PizzaService $service) > { > $this->service = $service; > } > > public function getPizzaService() > { > return $this->service; > } > > public function __invoke() > { > $random = $this->getPizzaService()->fetchSingleByRandom(); > > $html = '<div class="hero-unit">'; > $html.= '<h1>' . current($random) . '</h1>'; > $html.= '</div>'; > > return $html; > } > } > ------------------------------------------------------------------------ > > Would you say this view helper is an example for good or for bad practice? > > Thanks for your comments. > > Regards, > > Ralf > > -- > List: [email protected] > Info: http://framework.zend.com/archives > Unsubscribe: [email protected] > > >
