我在 ZF2 中有登录和退出系统。我想在他/她登录时显示登录用户的用户名。屏幕截图如下:
我有不同的观点,例如view / provinces / index.phtml,view / districts / index.phtml等。
我在view / layout / layout.phtml中有layout.phtml,其中我描述了admin的布局,适用于每个视图。因此,有必要在layout.phtml中访问登录用户的用户名。
我还有相应的控制器,如Controller / ProvincesController.php,Controller / DistrictsController.php等。我可以在Controller / ProvincesController.php中访问登录用户的用户名等等代码:
public function getAuthService()
{
$this->authservice = $this->getServiceLocator()->get('AuthService');
return $this->authservice;
}
$username = $this->getAuthService()->getStorage()->read();
但我无法在layout.phtml中访问登录用户用户名的值。
因此,如果有人知道它或有简单的想法或做法,请告诉我。
Module.php:
<?php
namespace Admin;
use Admin\Model\Profile;
use Admin\Model\ProfileTable;
use Admin\Model\Provinces;
use Admin\Model\ProvincesTable;
use Admin\Model\Districts;
use Admin\Model\DistrictsTable;
use Admin\Model\User;
use Admin\Model\UserTable;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\Authentication\Adapter\DbTable as DbTableAuthAdapter;
use Zend\Authentication\AuthenticationService;
class Module implements AutoloaderProviderInterface
//class Module
{
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function getServiceConfig()
{
return array(
'abstract_factories' => array(),
'aliases' => array(),
'factories' => array(
// SERVICES
'AuthService' => function($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$dbTableAuthAdapter = new DbTableAuthAdapter($dbAdapter, 'user','username','password', 'MD5(?)');
$authService = new AuthenticationService();
$authService->setAdapter($dbTableAuthAdapter);
return $authService;
},
// DB
'UserTable' => function($sm) {
$tableGateway = $sm->get('UserTableGateway');
$table = new UserTable($tableGateway);
return $table;
},
'UserTableGateway' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new User());
return new TableGateway('user', $dbAdapter, null,
$resultSetPrototype);
},
// FORMS
'LoginForm' => function ($sm) {
$form = new \Admin\Form\LoginForm();
$form->setInputFilter($sm->get('LoginFilter'));
return $form;
},
// FILTERS
'LoginFilter' => function ($sm) {
return new \Admin\Form\LoginFilter();
},
'Admin\Model\ProvincesTable' => function($sm) {
$tableGateway = $sm->get('ProvincesTableGateway');
$table = new ProvincesTable($tableGateway);
return $table;
},
'ProvincesTableGateway' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Provinces());
return new TableGateway('provinces', $dbAdapter, null, $resultSetPrototype);
},
'Admin\Model\DistrictsTable' => function($sm) {
$tableGateway = $sm->get('DistrictsTableGateway');
$table = new DistrictsTable($tableGateway);
return $table;
},
'DistrictsTableGateway' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Districts());
return new TableGateway('districts', $dbAdapter, null, $resultSetPrototype);
},
),
'invokables' => array(),
'services' => array(),
'shared' => array(),
);
}
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
// if we're in a namespace deeper than one level we need to fix the \ in the path
__NAMESPACE__ => __DIR__ . '/src/' . str_replace('\\', '/' , __NAMESPACE__),
),
),
);
}
}
提前致谢。
答案 0 :(得分:2)
推荐的方法是使用identity
(https://framework.zend.com/manual/2.4/en/modules/zend.view.helpers.identity.html)视图助手。然后在任何视图模型中,您可以按如下方式使用它:
if ($user = $this->identity()) {
echo 'Logged in as ' . $this->escapeHtml($user->getUsername());
} else {
echo 'Not logged in';
}
为了使其正常工作,您必须以特定名称Zend\Authentication\AuthenticationService
注册您的身份验证服务。
因此,在module.config.php
文件中,添加到service_manager
:
'service_manager' => array(
'aliases' => array(
'Zend\Authentication\AuthenticationService' => 'AuthService', // <--- this line
),
'invokables' => array(
'AuthService' => 'Your\Authentication\Class',
),
),
然后你应该可以使用identity
控制器插件并查看助手。
在您的情况下,Module.php
应如下所示:
...
public function getServiceConfig()
{
return array(
'abstract_factories' => array(),
'aliases' => array(
'Zend\Authentication\AuthenticationService' => 'AuthService', // <--- this line
),
'factories' => array(
// SERVICES
'AuthService' => function($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$dbTableAuthAdapter = new DbTableAuthAdapter($dbAdapter, 'user','username','password', 'MD5(?)');
$authService = new AuthenticationService();
$authService->setAdapter($dbTableAuthAdapter);
return $authService;
},
// DB
'UserTable' => function($sm) {
$tableGateway = $sm->get('UserTableGateway');
$table = new UserTable($tableGateway);
return $table;
},
'UserTableGateway' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new User());
return new TableGateway('user', $dbAdapter, null,
$resultSetPrototype);
},
// FORMS
'LoginForm' => function ($sm) {
$form = new \Admin\Form\LoginForm();
$form->setInputFilter($sm->get('LoginFilter'));
return $form;
},
// FILTERS
'LoginFilter' => function ($sm) {
return new \Admin\Form\LoginFilter();
},
'Admin\Model\ProvincesTable' => function($sm) {
$tableGateway = $sm->get('ProvincesTableGateway');
$table = new ProvincesTable($tableGateway);
return $table;
},
'ProvincesTableGateway' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Provinces());
return new TableGateway('provinces', $dbAdapter, null, $resultSetPrototype);
},
'Admin\Model\DistrictsTable' => function($sm) {
$tableGateway = $sm->get('DistrictsTableGateway');
$table = new DistrictsTable($tableGateway);
return $table;
},
'DistrictsTableGateway' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Districts());
return new TableGateway('districts', $dbAdapter, null, $resultSetPrototype);
},
),
'invokables' => array(),
'services' => array(),
'shared' => array(),
);
}
...
然后在你的布局或任何其他.phtml文件中:
layout.phtml
...
<?php if ($this->identity()): ?>
<p>Welcome, <?php echo $this->identity()->getUsername(); ?></p>
<?php endif; ?>
答案 1 :(得分:0)
在SzymonM先生的回答帮助下,我按照他的建议更改了我的Module.php,并在layout.phtml中编写以下简单代码
这解决了我的问题,用大写字母的第一个字母显示了用户名。
<?php
if ($this->identity())
{
echo ucfirst($this->identity());
}
?>
//ucfirst is php function which make first letter Uppercase.