In attempting to be a "purist" of sorts in terms of not introducing unnecessary dependencies in my code, I am attempting to use a view helper to access a model and bring in the data. For a simple example, I have a model, Model_Twitter, which is essentially a wrapper for the Zend_Service_Twitter client:
class Model_Twitter
{
protected $_service = null;
public function fetchLatestTweets($num = 10)
{
return $this->getService()->status->userTimeline(array('count' => $num));
}
}
Obviously a significant—and irrelevant—amount of the model has been snipped for brevity.
To access the Twitter updates in a view I create a view helper.
class Zend_View_Helper_LatestTweets extends Zend_View_Helper_Abstract
{
public function latestTweets($count) {
//here is the problem I am trying to solve
$twitterModel = new Model_Twitter($configurationAsHardcodedArray);
$this->view->tweets = $twitterModel->fetchLatestTweets($count);
return $this->view->render('sidebar/latest-tweets.phtml');
}
}
What I would like to do is to get a configuration object (that is created during bootstrapping) into the view helper that holds the credentials, etc. for the Twitter service model _without_ stuffing it into the Registry. The solutions that come to mind are:
1. Injecting the necessary resource—in this case the configuration object—into the view, but this smells horrible.
2. Creating a view helper to access the configuration object. But if I can access the config object in one view helper, why don't I just give that same access to the current resource.
3. Extending my view helpers from a base view helper that has a dependency container for such purposes.
4. None of the above.
5. All of the above.
6. Some combination hereof.
What solution would you use? Am I making this harder than it should be?
Thank you for pointers and insight.
Regards,
- Jeff
没有评论:
发表评论