3
votes

I'm using ZfcUser to Authenticate. Currently trying to tell if a user is logged in the layout.phtml file using ...

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

I'm guessing I need to add some path to the application config file?

4
$this->zfcUserAuthentication() is a call to a plugin in ActionController. You need to pass it to a viewScript or just use a viewHelper as @Mark T wrote in his answer.MFix

4 Answers

4
votes

You can use the view helper supplied with ZfcUser (i.e. the one that you referenced in your question, mtbikemike). Use dynamic injection to load the view helper by adding the following into the configuration of your module (module.config.php). You may need to integrate the code below with any existing contents of module.config.php 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',
                    ),
                ),
            ),
        ),
    ),
);

That loads two view helpers, so if you only want the zfcUserIdentity helper then you can always remove the reference to zfcUserLoginWidget.

4
votes

I'm new to Zend Framework 2 (and ZfcUser), so I don't know how it worked when you posted your question. Right now it's as easy as:

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

It's defined in getViewHelperConfig() function in ZfcUser's Module.php. If the user is logged, it will return an instance of ZfcUserDoctrineORM\Entity\User, otherwise a false.

2
votes

Some view helper examples: How to check if the user is logged in

<!-- 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 ?>
0
votes

I don't know if this is the 'correct' way, but you can add the identity as a ViewModel variable of the layout. Put the following in your 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';                
}