Embedding a permanent Zend login form

I already saw that this question has already been asked - but none of the answers actually sang for me, so I ask again: I want to insert a permanent registration form (which will change in the navigation panel if it logs in) into the panel header for the site. In fact, I want to be able to insert some controller logic into the layout.

After much research, I see several ways to achieve this goal - none of them seem ideally suited.

View Helpers seem to be suitable for adding a set of methods to a Zend_View object, but I don't want to write conditional code in layout.phtml to run the method. The action assistants will help me remove this functionality and call it from the Controller - but it seems to be in a bad state for several blocks. Then there are plugins that can be well matched in the send / authentication loop.

So, I was hoping that someone would be able to offer me some tips on how best to satisfy my requirements. Any help is appreciated.

+3
source share
1 answer

For those of you who have a similar problem, here is how I solved it (I use the btw layout)

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 - , .

, !

+1

Source: https://habr.com/ru/post/1728174/


All Articles