Wednesday, 4 September 2013

How can I use Sessions and Auth cross controller?

How can I use Sessions and Auth cross controller?

I have two controllers that need to communicate OrdersController and
UsersController, because when a new user places an order I process a
register form on the order that i send to UsersController::register().
My register action uses Auth to log in the user after the basket is
processed but it returns Fatal error: Call to a member function login() on
a non-object:
//UsersController::register($data = null)
if(!empty($data))
$this->request->data['User'] = $data;
if ($this->User->save($this->request->data)) {
$registeredUser = $this->User->read(null, $this->User->id);
if ($this->Auth->login($registeredUser['User']))
if (empty($data))
$this->redirect($this->Auth->redirect());
else
return $this->User->id;
}
I use this action on the normal registration page and also for other apis
like Google or Facebook inside the UsersController calling it with
$this->register($data) and it workes fine. But when I use it in the
OrdersController like:
//UsersController::__construct()
public function __construct($request = null, $response = null, $session =
null) {
parent::__construct($request, $response);
if (isset($session))
$this->Session = $session;
}
I had to override __construct because I had the same problem with setFlash()
//OrdersController::add();
$newUser = new UsersController(null, null, $this->Session);
$newUserId = $newUser->register($data['User']);

No comments:

Post a Comment