Cakephp 如何访问用户';是否从default.ctp获取s ID?
一旦用户登录,他们就可以使用profile按钮,该按钮会将他们重定向到他们的profile。但是,我无法获取default.ctp来访问当前用户。我想这是我得到的最接近的结果:Cakephp 如何访问用户';是否从default.ctp获取s ID?,cakephp,Cakephp,一旦用户登录,他们就可以使用profile按钮,该按钮会将他们重定向到他们的profile。但是,我无法获取default.ctp来访问当前用户。我想这是我得到的最接近的结果: <?php if($loggedIn) : ?> <li><?= $this->Html->link('Search', ['controller' => 'users', 'action' => 'search']); ?></li> <li
<?php if($loggedIn) : ?>
<li><?= $this->Html->link('Search', ['controller' => 'users', 'action' => 'search']); ?></li>
<li><?= $this->Html->link('Profile', ['controller' => 'users', 'action' => 'profile', $this->request->getSession()->read('Auth.User.Id')]); ?></li>
<li><?= $this->Html->link('Settings', ['controller' => 'users', 'action' => 'settings']); ?></li>
<li><?= $this->Html->link('Logout', ['controller' => 'users', 'action' => 'logout']); ?></li>
<?php else : ?>
<li><?= $this->Html->link('Register', ['controller' => 'users', 'action' => 'register']); ?></li>
<li><?= $this->Html->link('Login', ['controller' => 'users', 'action' => 'login']); ?></li>
<?php endif; ?>
我曾尝试在AppController中使用类似的代码,包括beforeRender和beforeFilter,但似乎没有任何效果。我感觉自己非常接近,可能只是使用了错误的组合。对CakePHP(3.8)来说是相当新的,因此任何帮助/提示都将不胜感激 在view.ctp中访问所有用户数据:
$user = $this->getRequest()->getSession()->read('Auth.User');
然后
或
如果您需要控制器中的身份验证用户数据:
$this->loadComponent('Auth');
$user = $this->Auth->user();
public function beforeFilter(Event $event)
{
$user = $this->request->getSession()->read('Auth.User');
if(!empty($user)) {
$this->set('user', $user);
}
}
public function profile($id = null)
{
$user = $this->Users->get($id, [
'contain' => ['Profiles']
]);
$this->set('user', $user);
}
public function search($id = null)
{
$user = $this->Users->get($id, [
'contain' => ['Profiles']
]);
$this->set('user', $user);
}
public function settings($id = null)
{
$user = $this->Users->get($id, [
'contain' => ['Profiles']
]);
$this->set('user', $user);
}
我花了比我需要的更多的时间在这个问题上,但当我发布这个问题时,我的下一次尝试解决了它 在AppController中:
$this->loadComponent('Auth');
$user = $this->Auth->user();
public function beforeFilter(Event $event)
{
$user = $this->request->getSession()->read('Auth.User');
if(!empty($user)) {
$this->set('user', $user);
}
}
public function profile($id = null)
{
$user = $this->Users->get($id, [
'contain' => ['Profiles']
]);
$this->set('user', $user);
}
public function search($id = null)
{
$user = $this->Users->get($id, [
'contain' => ['Profiles']
]);
$this->set('user', $user);
}
public function settings($id = null)
{
$user = $this->Users->get($id, [
'contain' => ['Profiles']
]);
$this->set('user', $user);
}
在default.ctp中:
<li><?= $this->Html->link('Search', ['controller' => 'users', 'action' => 'search', $user['user_id']]); ?></li>
<li><?= $this->Html->link('Profile', ['controller' => 'users', 'action' => 'profile', $user['user_id']]); ?></li>
<li><?= $this->Html->link('Settings', ['controller' => 'users', 'action' => 'settings', $user['user_id']]); ?></li>