2010-01-12 58 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的动作助手有利的文章阅读一些不太之后 - 我选择了这种方法,并获得成功。

希望有帮助!