我对zend框架很新,并且在我的小应用程序中添加会话但我不知道如何将会话变量打印到我的header.phtml页面。
UsersTable.php
public function fetchbyWhere($where) {
$rowset = $this->tableGateway->select($where);
$row = $rowset->current();
if (!$row) {
return;
}
return $row;
}
IndexController.php
<?php
namespace Application\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Application\Model\Users; // <-- Add this import
use Zend\Session\Container; // We need this when using sessions
class IndexController extends AbstractActionController {
protected $usersTable;
public function getUsersTable() {
if (!$this->usersTable) {
$sm = $this->getServiceLocator();
$this->usersTable = $sm->get('Application\Model\UsersTable');
}
return $this->usersTable;
}
public function indexAction() {
$request = $this->getRequest();
if ($request->isPost()) {
$user = $request->getPost('txtuser');
$pass = $request->getPost('txtpassword');
$wher = array('username' => $user, 'password' => $pass);
$resultSet = $this->getUsersTable()->fetchbyWhere($wher);
//var_dump($resultSet);
if($resultSet)
{
$user_session = new Container('user');
$user_session->ses_user = $resultSet->username;
return new ViewModel(array(
'msg' => 'valid user',
'sesuser' => $user_session->ses_user,
));
}
else {
return new ViewModel(array(
'msg' => 'not a valid user',
));
}
} else {
return new ViewModel();
}
}
}
现在我不知道如何在header.phtml页面上打印此会话。
答案 0 :(得分:2)
您还需要在头文件中添加会话容器。
在标题文件中添加以下行。
<?php
use Zend\Session\Container; // We need this when using sessions
$user_session = new Container('user');
if(isset($user_session->ses_user))
echo "user:".$user_session->ses_user;
?>