简体   繁体   English

在zend Framework 2.5.1中的layout.phtml中显示用户名

[英]display username in layout.phtml in zend framework 2.5.1

I am new in zend and using zend framework 2.5.1. 我是zend的新手,并使用zend Framework 2.5.1。 Using "authservice" i have done login authentication in my project. 使用“ authservice”,我已经在项目中完成了登录身份验证。 I am able to fetch login detail by using $this->getAuthService()->getIdentity(); 我可以使用$ this-> getAuthService()-> getIdentity();来获取登录详细信息。 in my controller. 在我的控制器中。 But i want to use it in each and every view page (layout). 但是我想在每个视图页面(布局)中使用它。 So that i could manage session but i am unable to do this. 这样我可以管理会话,但我无法执行此操作。 Moreover i want to display logged in username in layout.phtml(or header.phtml). 此外,我想在layout.phtml(或header.phtml)中显示登录的用户名。 I want to show logged in user name like "Welcome ABC". 我想显示登录的用户名,例如“ Welcome ABC”。 So please help me to solve this problem. 所以请帮我解决这个问题。

See the Identity view helper for in your view files like: layout.phtml or page specific ones. 请在您的视图文件中查看Identity视图帮助器,例如: layout.phtml或页面特定的文件。

Just like the document states: 就像文档中指出的那样:

if ($user = $this->identity()) {
    echo $this->translate('Welcome') . ' ' . $this->escapeHtml($user->getUsername());
} else {
    echo $this->translate('Welcome guest');
}

I use a custom view helper to achieve this, passing the Auth service via factory. 我使用自定义视图助手来实现此目的,并通过工厂传递Auth服务。

My view helper 我的视图助手

namespace Application\View\Helper;

use Zend\View\Helper\AbstractHelper;
use Zend\Authentication\AuthenticationService;

class WelcomeUser extends AbstractHelper
{

    private $welcomeUser;

    /**
     *
     * @param AuthenticationService $auth
     */
    public function __construct(AuthenticationService $auth)
    {
        $this->welcomeUser = 'Guest';
        if ($auth->hasIdentity()) {
            $user = $auth->getIdentity();
            $this->welcomeUser = $user->getFirstLastName();
        }
    }

    /**
     *
     * @return string
     */
    public function __invoke()
    {
        return $this->welcomeUser;
    }

}

And it's factory 这是工厂

namespace Application\View\Helper\Service;

use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Application\View\Helper\WelcomeUser;
use Zend\Authentication\AuthenticationService;

class WelcomeUserFactory implements FactoryInterface
{

    /**
     *
     * @param ServiceLocatorInterface $serviceLocator
     * @return WelcomeUser
     */
    public function createService(ServiceLocatorInterface $serviceLocator)
    {
        return new WelcomeUser($serviceLocator->getServiceLocator()->get(AuthenticationService::class));
    }

}

Don't forget to register you view helper in module.config.php 不要忘记在module.config.php中注册您的视图助手

'view_helpers' => array(
        'factories' => array(
            'welcomeUser' => 'Application\View\Helper\Service\WelcomeUserFactory',
        ),
    ),

Finally in your layout.phtml use <?php echo $this->welcomeUser(); ?> 最后在您的layout.phtml中使用<?php echo $this->welcomeUser(); ?> <?php echo $this->welcomeUser(); ?>

I hope this point you in the right direction. 我希望这能为您指明正确的方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM