Zend Framework2 - Problems with AbstractHelper

0

I'm having trouble displaying the logged-in username using view helper.

Module:

public function getViewHelperConfig() {
    return array(
        'invokables' => array(
            'UserIdentity' => new View\Helper\UserIdentity()
        )
    );
}

UserIdentity:

use Zend\View\Helper\AbstractHelper;
use Zend\Authentication\AuthenticationService,
    Zend\Authentication\Storage\Session as SessionStorage;

class UserIdentity extends AbstractHelper {

    protected $authService;

    public function getAuthService() {
        return $this->authService;
    }

    public function __invoke($namespace = null) {
        $sessionStorage = new SessionStorage($namespace);
        $this->authService = new AuthenticationService;
        $this->authService->setStorage($sessionStorage);

        if ($this->getAuthService()->hasIdentity()) {
            return $this->getAuthService()->getIdentity();
        } else {
            return false;
        }
    }

}

View:

$usuario = $this->UserIdentity('Usuario');
    
asked by anonymous 09.03.2015 / 02:06

1 answer

0

Your plugin configuration is incorrect. Instead of instantiating your helper, define it as follows:

<?php

namespace Application;

use Zend\ModuleManager\Feature\ViewHelperProviderInterface;

class Module implements ViewHelperProviderInterface
{

  public function getViewHelperConfig() {
    return array(
      'invokables' => array(
        'UserIdentity' => 'Application\View\Helper\UserIdentity'
      )
    );
  }

}

Also check out the consistency of your namespaces.

I hope I have helped! : D

    
21.05.2015 / 17:41