3

私はこの質問がすでに尋ねられているのを見ましたが、答えはどれも私にとって本当にゲル化していないので、もう一度尋ねます:永続的なログインフォーム(ログインするとナビゲーションバーに変わります)をヘッダーバーに埋め込みたいサイトの場合。事実上、いくつかのコントローラーロジックをレイアウトに挿入できるようにしたいと思います。

多くの調査の結果、これを達成する可能性のあるいくつかの方法を見つけることができますが、どれも理想的には適していないようです。

ビューヘルパーは、一連のメソッドをZend_Viewオブジェクトに追加するのに適しているようですが、layout.phtmlにメソッドをトリガーする条件付きコードを記述したくありません。アクションヘルパーは、その機能を削除してコントローラーから呼び出すのに役立ちますが、それはいくつかの四半期からはあまり好まれていないようです。次に、ディスパッチ/認証ループに適している可能性のあるプラグインがあります。

ですから、私は誰かが私の要件に最も適した方法についてのガイダンスを提供してくれることを望んでいました。どんな助けでも大歓迎です。

4

1 に答える 1

1

似たような問題を抱えている方のために、これが私がそれを解決することになった方法です(私はレイアウト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 Action Helperに関するいくつかのあまり好ましくない記事を読んだ後、私はこの方法を選択しました。

お役に立てば幸いです。

于 2010-04-14T08:36:07.323 に答える