用户控制器中的登录功能
public function login() {
if ($this->request->is('post')) {
$user = $this->Auth->identify();
// print_r($user);
// die();
if ($user['role'] === 'student') {
$this->Auth->setUser($user);
$session = $this->request->session();
$session->write('user', $user);
return $this->redirect(['controller' => 'Useracountinfo/addinfo']);
} elseif .....
和我在Useracountinfo中的添加信息功能
public function addinfo()
{
$this->loadModel('Users');
$userinfo= $this->Users->find('all');
$session = $this->request->session();
$userinfo = $session->read('user');
//print_r($userinfo);
//die();
$this->set($userinfo);
$user = $this->Useracountinfo->newEntity();
if ($this->request->is('post')) {
$user = $this->Useracountinfo->patchEntity($userinfo, $this->request->data);
print_r($user);
die();
if ($this->Useracountinfo->save($user)) {
$this->Flash->success(__('The user has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The user could not be saved. Please, try again.'));
}
}
$this->set(compact('useracountinfo'));
$this->set('_serialize', ['useracountinfo']);
}`
在我看来我有类似的东西
<?php echo '<strong>'.$userinfo['email'].'</strong>'; ?>
答案 0 :(得分:0)
正确的语法是(参见MongoDB documentation)
$this->set('variable_name', somevalue);
所以在你的情况下
$this->set('userinfo', $userinfo);
请注意,这不是一个会话变量,只是一个php变量,它将控制器共享到视图
如果您希望在每个视图中都可以使用此功能,则可以在AppController
但您甚至不需要这样做,因为$this->request->session()
在视图中也始终可用
这说我不完全明白你想要实现的目标,所以也许有更好的解决方案