ZF2: how to find out if a user is registered in ZfcUser in the layout.phtml file

I use ZfcUser for authentication. Currently trying to determine if the user is registered in the layout.phtml file using ...

<? if ($this->zfcUserAuthentication()->hasIdentity()): ?>

I assume that I need to add some path to the application configuration file?

+3
source share
4 answers

You can use the view helper provided with ZfcUser (i.e. the one you specified in your question, mtbikemike ). Use dynamic injection to load the view helper by adding the following to your module configuration (module.config.php). You may need to integrate the code below with any existing module.config.php content, of course.

return array(
    'di' => array(
        'instance' => array(
            'Zend\View\HelperLoader' => array(
                'parameters' => array(
                'map' => array(
                        'zfcUserIdentity' => 'ZfcUser\View\Helper\ZfcUserIdentity',
                        'zfcUserLoginWidget' => 'ZfcUser\View\Helper\ZfcUserLoginWidget',
                    ),
                ),
            ),
        ),
    ),
);

, , zfcUserIdentity, zfcUserLoginWidget.

+4

Zend Framework 2 ( ZfcUser), , , . :

<?php if ( $this->zfcUserIdentity() ) { ?>

getViewHelperConfig() ZfcUser Module.php. , ZfcUserDoctrineORM\Entity\User, - false.

+4

Some examples of supporting examples: How to check if a user is registered

<!-- 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 ?>
+2
source

I do not know if this is the "right" way, but you can add it as a ViewModel variable of the layout. Put the following in the module class:

public function init(Manager $moduleManager)
{
    $events = $moduleManager->events();
    $sharedEvents = $events->getSharedCollections();
    $sharedEvents->attach('bootstrap', 'bootstrap', array($this, 'initializeView'), 100);
}

public function initializeView($e)
{
    $app = $e->getParam('application');  
    $viewModel = $app->getMvcEvent()->getViewModel();
    $viewModel->myLayoutVariable = 'myLayoutVariable';                
}
0
source

All Articles