2010-01-12 51 views
3

我已經看到這個問題已經問 - 但沒有答案真的凝聚着我,所以我再次問:我想嵌入一個持久登錄表單(這將改變成一個導航欄如果登錄)在網站的標題欄中。實際上,我希望能夠在佈局中注入一些控制器邏輯。嵌入持久登錄表單Zend

經過大量研究,我可以看到幾種可能達到此目的的方法 - 其中沒有一種看起來非常合適。

視圖助手似乎很適合向Zend_View對象添加一套方法 - 但我不想在layout.phtml中編寫條件代碼來觸發方法。行動助手會幫助我刪除這個功能,並從控制器中調用它 - 但是這似乎在幾個方面都不太好。然後是插件,這可能非常適合於調度/認證循環。

所以,我希望有人能夠爲我提供一些指導,以最適合我的要求的方式。任何幫助是極大的讚賞。

回答

1

對於那些你用similair的問題,這是我的最後解決它(我使用的佈局BTW)

我註冊在引導一個視圖助手:

protected function _initHelpers(){ 
    //has to come after view resource has been created 
    $view = $this->getResource('view'); 
    // prefix refers to the folder name and the prefix for the class 
    $view->addHelperPath(APPLICATION_PATH.'/views/helpers/PREFIX','PREFIX'); 
    return $view; 
} 

這裏的視圖助手代碼 - 實際的身份驗證邏輯隱藏在模型代碼中。這是一個有點笨拙,但它的工作原理

class SB_UserLoginPanel extends Zend_View_Helper_Abstract { 

public function __construct() { 
    $this->user = new SB_Entity_Users(); 
$this->userAccount = new SB_Model_UserAccount(); 
    $this->request = Zend_Controller_Front::getInstance()->getRequest(); 
    $this->form = $this->makeLoginForm(); 
    $this->message=''; 
} 

//check login 
public function userLoginPanel() { 
    if(isset($_POST['loginpanel']['login'])) { 
     $this->processLogin(); 
    } 
    if(isset($_POST['loginpanel']['logout'])) { 
     $this->processLogout(); 
    } 
    $auth = Zend_Auth::getInstance(); 
    if ($auth->hasIdentity()) { 
     $this->loginPanel = $this->getUserNav(); 
    } else { 
     $this->loginPanel = $this->getLoginForm(); 
     $this->loginPanel .= $this->getMessages(); 
    } 
    return $this->loginPanel; 
} 

private function processLogin() { 
    if($this->form->isValid($_POST)){ 
     $logindata = $this->request->getPost('loginpanel'); 
     if($this->user->login($logindata['email'],$logindata['password'])) { 
      Zend_Session::rememberMe(); 
      $redirect = new Zend_Controller_Action_Helper_Redirector(); 
      $redirect->goToUrl('/account/'); 
      return $this->getUserNav(); 
     }else { 
      $this->message = '<p id="account_error">Account not authorised</p>'; 
     } 
    }else { 
     $this->form->getMessages(); 
    } 
} 


private function processLogout() { 
    if(isset($_POST['loginpanel']['logout'])) { 
     $this->user->logout(); 
     $request_data = Zend_Controller_Front::getInstance()->getRequest()->getParams(); 
     if($request_data['controller']=='notallowed') { 
      $redirect = new Zend_Controller_Action_Helper_Redirector(); 
      $redirect->goToUrl('/'); 
     } 
    } 
} 

private function makeLoginForm() { 
} 

private function getLoginForm(){ 
    return $this->form; 
} 

private function getMessages(){ 
    return $this->message; 
} 

private function getUserNav(){   
//return partial/render 
} 

}

然後我從layout.phtml文件標記的相關部分進行調用。

<?php echo $this->doctype(); ?> 
<head> 
<?php 
echo $this->headLink() ."\n"; 
echo $this->headScript() ."\n"; 
echo $this->headMeta() ."\n"; 
?> 
<title><?php echo $this->escape($this->title) ."\n"; ?></title> 
</head> 
<div id="masthead"> 
    <div id="userLoginPanel"> 
     <?php echo $this->userLoginPanel(); ?> 
    </div> 
</div> 
<!--rest of layout--> 

原則上,這應該是一個動作助手,但不是把Zend的動作助手有利的文章閱讀一些不太之後 - 我選擇了這種方法,並獲得成功。

希望有幫助!