Search code examples
twitter-bootstrapzend-framework2zfcuser

Dynamic Navigation depending on Login Status in Zend Framework 2


I've got a small login form in my top navbar with a input fields EMAIL, PASSWORD and a sign in button (see image below). How can I replace it depending on the login status from a user? If a user is logged in, a dropdown should appear where he can log out etc. (see image 2)
I'm using ZF2, zfc-user and Twitter Bootstrap 3.
My navbar code for the login widget: `

        $form = $this->zfcUserLoginWidget(array('render' => false))->getVariable('loginForm');
        $form->setAttribute('action', $this->url('zfcuser/login'));
        $form->setAttribute('method', 'post');
        $form->setAttribute('class', 'navbar-form navbar-right form-inline');
        echo $this->form()->openTag($form);
        echo $this->formElementErrors($form->get('identity')) ?>
        <div class="form-group">
            <?php echo $this->formInput($form->get('identity')) ?>
            <?php echo $this->formInput($form->get('credential')) ?>
            <?php if ($this->redirect): ?>
                <input type="hidden" name="redirect" value="<?php echo $this->escapeHtml($this->redirect) ?>"/>
            <?php endif ?>
            <?php echo $this->formButton($form->get('submit')); ?>
        </div>
        <?php
        echo $this->form()->closeTag();
        ?>

` Login Widget

What it should turn into after login:
enter image description here


Solution

  • Ok, the solution came to me while powernapping:

    <!-- Test if the User is connected -->
    <?php if(!$this->zfcUserIdentity()): ?>
    <!-- display the login form -->
    <?php echo $this->zfcUserLoginWidget(array('redirect'=>'application')); ?>
    <?php else: ?>
    <!-- display the 'display name' of the user -->
    <?php echo $this->zfcUserIdentity()->getDisplayname(); ?>
    <?php endif?>
    

    I just copy-pasted my code into the sections and it worked.