Hello !
I come back to you for a Session problem. I connect to the identification table, and I manage to retrieve the information from this table of the connected person. I would now like to be able to read all the information about the person. I can do it wihout a session, bit with the session, I can’ do it. I tried with write, but I don’t really undersant if it’s really that, because it displays string. Do you have a lead please ? Thanks !
Ps: I try to do it without using helpers. My mistake right now, and that it requires me to create a helper.
public function login()
{
if ($this->request->is(‘post’)) {
$user = $this->Auth->identify();
if ($user) {
$this->Auth->setUser($user);
return $this->redirect($this->Auth->redirectUrl(’./identification/welcome’));
} else {
$this->Flash->error(‘Nom d’utilisateur ou mot de passe incorrect’);
}
}
}
public function welcome()
{
$this->Auth->user(‘id’);
$this->Auth->user(‘username’);
$this->Auth->user(‘candidate.lastname’);
}
View :
$session = $this->getRequest()->getSession();
$session->write(‘Candidate’, $this->User->Candidate->findById($this->Auth->user(‘id’)));
<?= h($session->read('Auth.User.username')) ?>
<?= h($session->read('Auth.User.Candidate.lastname')) ?>
Hi
Try to post code with formatting, so its easier to read. See Creating and highlighting code blocks
You should read the session in the controller, and use View Variables
You should never use models/sessions directly in the view
something like this
// in controller
public function login()
{
if ($this->request->is(‘post’)) {
$user = $this->Auth->identify();
if ($user) {
$this->Auth->setUser($user);
return $this->redirect($this->Auth->redirectUrl(’./identification/welcome’));
} else {
$this->Flash->error(‘Nom d’utilisateur ou mot de passe incorrect’);
}
}
}
public function welcome()
{
$userId = $this->Auth->user('id');
$userName = $this->Auth->user('username');
$lastName = $this->Auth->user(‘candidate.lastname’);
$session = $this->getRequest()->getSession();
$candidate = $this->Users->Candidates->findById($userId)->first();
$session->write(‘Candidate’, $candiate);
$this->set(compact('userId', 'userName', 'lastName', 'candidate'));
}
In the template (welcome.ctp)
UserId <?= h($userId) ?>
userName <?= h($userName) ?>
Candidate <?= h($candidate->lastname) ?>
Thank you, I thought ``` it was only on discord
Okay. We don’t use read anymore?
With this code, it tells me: Call to a member function findById () on null.
I recover the value of $userId just the line before that of findById, but if I put the debug just below it does not recover it …
That depends on the name of the model.
If you have src/Model/Table/CandidatesTable.php
you can Call
$this->loadModel('Candidates');
$candidate = $this->Candidates->findById($userId)->first();
In your original code you had User instead of Users.
$this->User->Candidates->findById($userId)->first();
Most examples you’ll find will be following conventions, I’ll be easier to call your table Users
instead of User
You can use session read/write. It’s best on a controller/cell.