Zend Framework2 - Problems with Abstracthelper

Asked

Viewed 40 times

0

inserir a descrição da imagem aqui

I’m having trouble displaying the logged-in user name 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');

1 answer

0

Your plugin configuration is incorrect. Instead of instantiating your helper, set 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 the consistency of your namespaces.

I hope I helped! D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.