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: fw-general@lists.zend.com
Info: http://framework.zend.com/archives
Unsubscribe: fw-general-unsubscr...@lists.zend.com


Reply via email to