2009年1月29日星期四

Re: [fw-mvc] Best way to access global view variable from partialLoop?

-- Abraham Block <atblock@gmail.com> wrote
(on Thursday, 29 January 2009, 02:08 PM -0500):
> Just wondering, for those with singletonphobia (that's the opposite of
> singletonitis), what's a good way to not have to rely on the Zend_Auth
> singleton (assume you can wrap Zend_Auth into another class)

Inject your authentication object into a helper.

First, define the helper:

class My_View_Helper_LoginStatus extends Zend_View_Helper_Abstract
{
protected $_auth;

public function loginStatus()
{
return $this->getAuth()->hasIdentity();
}

public function setAuth(Zend_Auth $auth)
{
$this->_auth = $auth;
return $this;
}

public function getAuth()
{
return $this->_auth;
}
}

Then, in your bootstrap, push your auth object into the helper:

// First, get the helper object:
$helper = $view->getHelper('loginStatus');

// Now do the dependency injection:
$helper->setAuth($auth);

In your view scripts, you can then utilize the helper:

<?php if ($this->loginStatus()): ?>
Welcome!
<?php else: ?>
Please log in!
<?php endif ?>

> On Thu, Jan 29, 2009 at 2:04 PM, Hector Virgen <djvirgen@gmail.com> wrote:
>
> If you're using Zend_Auth, you don't need to access your controller:
>
> <?php
>
> $loggedin = Zend_Auth::getInstance()->hasIdentity();
>
> if (true === $loggedin) {
> // user is logged in
> }
>
> ?>
>
> If you're not using Zend_Auth, you may want to store your login info
> somewhere accessible, like the registry or session, which should be
> accessible from your view helper. Storing it in the controller locks the
> user into that controller.
>
> -Hector
>
>
>
> On Thu, Jan 29, 2009 at 10:51 AM, GJ Bogaerts <gj@zigg.nl> wrote:
>
>
> Hi Avi
>
> thanks, I thought of that. But that would need to be a view helper that
> directly accesses the controller, wouldn't it? Because that's where the
> login status is kept.
>
>
> Avi Block wrote:
> >
> > use a view helper
> >
> >
> >
> --
> View this message in context: http://www.nabble.com/
> Best-way-to-access-global-view-variable-from-partialLoop--tp21722277p21733248.html
> Sent from the Zend MVC mailing list archive at Nabble.com.
>
>
>
>
>

--
Matthew Weier O'Phinney
Software Architect | matthew@zend.com
Zend Framework | http://framework.zend.com/

没有评论: