Hi!

I have a Zend project and I would like to implement a login form in the header of the site in such a way, that if a user is not logged in, he can use the site, but the form is always there, no matter what pages he access. If he is logged in, then his username is always in the header.

Please advise some links for documentation on this subject.

Thank you.

Finally I have managed to solve this task and works as expected. Of course, it can be extended. Here it is a simple way.

application.ini

resources.frontController.actionhelperpaths.Application_Controller_Helper = APPLICATION_PATH "/controllers/helpers"

Bootstrap.php

protected function _initMyActionHelpers()
{
    $this->bootstrap('frontController');
    $login = Zend_Controller_Action_HelperBroker::getStaticHelper('Login');
    Zend_Controller_Action_HelperBroker::addHelper($login);
}

application/forms/Login.php

class Application_Form_Login extends Zend_Form
{
    public function init()
    {
            $this->addElement('text', 'username', array(
            'placeholder' => 'Userame',
            'required' => true,
            'validators' => array(
            array('StringLength', false, array('max'=>75))
            )
            ));
            $this->addElement('password', 'password', array(
            'placeholder' => 'Password',
            'required' => true,
            'validators' => array(
            array('StringLength', false, array('max'=>150))
            )
            ));
            $this->addElement('submit', 'submitlogin', array(
            'label' => 'Login'
            ));
    }
}

application/forms/Logout.php

class Application_Form_Logout extends Zend_Form
{
    public function init()
    {
        $this->addElement('submit', 'submitlogout', array(
        'label' => 'Logout'
        ));
    }
}

application/controllers/helpers/Login.php

class Application_Controller_Helper_Login extends Zend_Controller_Action_Helper_Abstract
{
    public function preDispatch()
    {
        $sessionUser = new Zend_Session_Namespace('user');
        $request = $this->getActionController()->getRequest();
        if(!isset($sessionUser->username)){
            $form = new Application_Form_Login();
            if($request->isPost() && $request->getPost('submitlogin')) {
                if($form->isValid($request->getPost())) {
                    $data = $form->getValues();
                    $sessionUser = new Zend_Session_Namespace('user');
                    $sessionUser->username = $data['username'];
                }
            }            
        } else {
            if($request->isPost() && $request->getPost('submitlogout')) {
                Zend_Session::namespaceUnset('user');
            }
        }
    }
}

application/views/helpers/Login.php

class Zend_View_Helper_Login extends Zend_View_Helper_Abstract
{
    public function Login()
    {
        $html = '';
        $sessionUser = new Zend_Session_Namespace('user');
        if(isset($sessionUser->username)) {
            $html = '<p>Welcome, ' . $sessionUser->username . '.</p>';
            $logout_form = new Application_Form_Logout();
            $html .= $logout_form;
        } else {
            $login_form = new Application_Form_Login();
            $html .= $login_form;
        }
        return $html;
    }
}

layout.phtml

<div id="header"><?php echo $this->Login($this->Login); ?></div>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.