views:

164

answers:

2

I've seen this question asked already - but none of the answers really gelled for me, so I'm asking again: I want to embed a persistent login form (which will change into a nav bar if logged in) in the header bar for a site. Effectively, I want to be able to inject some controller logic into the layout.

After much research, I can see several ways that might achieve this - none of which seem ideally suited.

View Helpers seem suited to adding a suite of methods to the Zend_View object - but I don't want to write conditional code in the layout.phtml to trigger a method. Action helpers would help me remove that functionality and call it from a Controller - but that seems to be in poor favour from several quarters. Then there are plugins, which might be well suited in the dispatch/authentication loop.

So, I was hoping someone might be able to offer me some guidance on which way might best suit my requirements. Any help is greatly appreciated.

A: 

Any chance of showing the code to do this?

snarfelborg
A: 

For those of you with a similair issue, this is how I ended up solving it (I'm using layout btw)

I registered a view helper in the 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;
}

Here's the view helper code - the actual authentication logic is tucked away in model code. It's a bit clumsy, but it works

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
}

}

I then call this from the relevant part of the markup in the layout.phtml file.

<?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-->

In principle, this should be an action helper, but after reading some less than favourable articles regarding Zend Action Helper - I opted for this method which did the trick.

Hope that helps!

sunwukung