Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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 :'(



77.7k
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!