3

我已经看到这个问题被问到了——但没有一个答案对我来说真的很清楚,所以我再次问:我想在标题栏中嵌入一个持久登录表单(如果登录,它将变成一个导航栏)对于一个网站。实际上,我希望能够将一些控制器逻辑注入到布局中。

经过大量研究,我可以看到几种可能实现这一目标的方法 - 没有一种方法看起来非常适合。

View Helpers 似乎适合向 Zend_View 对象添加一套方法——但我不想在 layout.phtml 中编写条件代码来触发方法。动作助手将帮助我删除该功能并从控制器中调用它——但这似乎在几个方面都不受欢迎。然后是插件,它们可能非常适合调度/身份验证循环。

所以,我希望有人可以为我提供一些指导,说明哪种方式最适合我的要求。任何帮助是极大的赞赏。

4

1 回答 1

1

对于那些有类似问题的人,这就是我最终解决它的方式(我正在使用布局顺便说一句)

我在 Bootstrap 中注册了一个视图助手:

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 Action Helper 的不太受欢迎的文章后 - 我选择了这种方法,它成功了。

希望有帮助!

于 2010-04-14T08:36:07.323 回答