星期五, 3月 18, 2011

Zend View Helper

利用View Helper,在網頁中呈現使用者登入的資訊
<html>
//header ...
$this->LoggedInUser->loggedInUser()();
//content ...
</html>

class Zend_View_Helper_LoggedInUser
{
    protected $_view;
    function setView($view){
        $this->_view = $view;
    }
    function loggedInUser(){
        $auth = Zend_Auth::getInstance();
        if($auth->hasIdentity()) #2{
            $logoutUrl = $this->_view->linkTo('auth/logout');
            $user = $auth->getIdentity(); #3
            $username = $this->_view->escape(ucfirst($user->name));
            $string = 'Logged in as ' . $username . ' | Log out';
        } else {
            $loginUrl = $this->_view->linkTo('auth/identify'); #4
            $string = 'Log in'; #5
        }
        return $string;
    }
}

沒有留言: