We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Session can't start

Hi everyone! I'm new in phalcon, and I'm trying to set the user's data to session, but i can't get. Here is what I have in my LoginController.

public function loginAction()
{
    if ($this->request->isPost()) 
    {
        $username = $this->request->getPost('username');
        $password = $this->request->getPost('password');
        $user = Usuarios::findFirst(array("username = :username: AND password = :password:",
            'bind' => array('username' => $username, 'password' => $password)));
        $usuario = $user->toArray();
        $resp["auth"] = $this->_registerSession($user);
        if ($user != false) {
            $this->_registerSession($user);
            return $this->response->redirect('/dashboard/');                
        }
    }
}

 public function _registerSession(Usuarios $user)
    {   
    $sesion = array(
        'iduser' => $user->iduser,
        'username' => $user->username,
        'nombre' => $user->nombre,
        'apellido' => $user->apellido,
        'email' => $user->email,
        'idpais' => $user->idpais,
        'ciudad' => $user->ciudad,
        'provincia' => $user->provincia,
        'numcelular' => $user->numcelular
    );

    $this->session->get('auth', $sesion);
    return $sesion;            

}
 public function endAction()
{
    $this->session->remove('auth');
    $this->flash->success('Goodbye!');
    $this->session->destroy();

    return $this->dispatcher->forward(
        [
            "controller" => "login",
            "action"     => "index",
        ]
    );
}

And DashboardController this:

    $iduser = $this->session->get('auth')["iduser"]; 
    $user = Usuarios::findFirst("iduser = ".$iduser);
    $usuario=$user->toArray(); 
    $this->view->usuario = $usuario; 

Butttt in my DashboardView i can't view nothing! Someone can help me :'(



85.5k
Accepted
answer

 public function _registerSession(Usuarios $user)
    {   
    $sesion = array(
        'iduser' => $user->iduser,
        'username' => $user->username,
        'nombre' => $user->nombre,
        'apellido' => $user->apellido,
        'email' => $user->email,
        'idpais' => $user->idpais,
        'ciudad' => $user->ciudad,
        'provincia' => $user->provincia,
        'numcelular' => $user->numcelular
    );

    $this->session->get('auth', $sesion);  # Perhaps you want $this->session->set('auth', $sesion);  ?
    return $sesion;            

}

¡Thanks very much!