-- Philip G <[EMAIL PROTECTED]> wrote
(on Wednesday, 19 September 2007, 12:41 PM -0500):
> I'm trying to figure out a few things here, and I've come to a road block.
>
> Here's what I'm trying to do: using a controller plugin, I check the
> idendity of a user and then if it exists, inject the data into the
> view class for access within the templates, but it's failing for me.
>
> I found an old discussion, dating back in June/July, talking about how
> to access to the controller created view object, which I've
> successfully implemented. However, adding any variables to that view
> class fails.
>
> This is my controller plugin piece:
> -------------------
> public function dispatchLoopShutdown() {
> if ( Zend_Auth::getInstance()->hasIdentity() ) {
> $viewRenderer =
> Zend_Controller_Action_HelperBroker::getExistingHelper('viewRenderer');
> $view = $viewRenderer->view;
>
> $identity = Zend_Auth::getInstance()->getIdentity();
> $view->user = $identity;
> $view->testVar = "This is a test";
> }
> }
> ------------------------
>
> In my file, I have:
> ---------------------------------------
> File: index/index.phtml
>
> <? print_r($this->user) ?>
> <? print_r($this->testVar) ?>
> ------------------------------------------
>
> I hit the file correctly, but I get no values within the template.
You've got the order wrong. At dispatchLoopShutdown(), the action has
already been performed, and the template already rendered -- the way you
have it now, you're assigning variables to the view object, but then not
rendering anything afterwards (index/index.phtml was rendered *before*
this plugin action was ever invoked).
Make the plugin operate at either dispatchLoopStartup() or preDispatch()
(the first will be executed exactly once, the second during each
iteration of the dispatch loop).
> I checked the Zend_View documents, but it feels horribly outdated or
> really incomplete as it makes absolutely no mention on how to access
> the view object created by the Controller class and use it in multiple
> location (ie: controller plug, different controllers via "forward"
> option, etc). I was thinking Zend_View was built as a singleton, but
> getInstance() fails.
You're actually doing it right. The ViewRenderer is registered at the
beginning of the dispatch loop (or earlier, if you do it in your
bootstrap), and then injects the view object into the action controllers
as they're dispatched. So, the way to reliably get the view object is as
follows:
$view =
Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')->view;
--
Matthew Weier O'Phinney
PHP Developer | [EMAIL PROTECTED]
Zend - The PHP Company | http://www.zend.com/