Reputation: 1805
I am trying to set up a view for my logged in user. I lifted the code from the cakephp 2.0 manual. this is the controller code:
public function view($id = null) {
$this->User->id = $id;
if (!$this->User->exists()) {
throw new NotFoundException(__('Invalid user'));
}
$this->set('user', $this->User->read(null, $id));
}
When a user with the user_id of 1 logs in, cake doesn't go to /users/1. Instead it just goes to /users/view. Here are the routes pertaining to the users controller:
Router::connect('/users', array('controller' => 'users', 'action' => 'login'));
Router::connect('/users/:action', array('controller' => 'users'));
Router::connect('/users/:id', array('controller' => 'users', 'action' => 'view'));
I'm not sure which I have wrong, the route code or the controller code.
UPDATE New controller function:
public function view($id = null) {
$this->User->id = $id;
if (!$this->User->exists()) :
throw new NotFoundException(__('Invalid user'));
elseif($this->Auth->login()) :
return $this->redirect(array('controller' => 'users', 'action' => 'view', AuthComponent::user('id')));
endif;
$this->set('user', $this->User->read(null, $id));
}
Upvotes: 0
Views: 719
Reputation: 34837
The page Cake opens after a succesful login is either the page the unauthenticated user tried to open initially or the action set in the loginRedirect
property of your Auth component in the (App)Controller. As also documented here. In other words, the above code has nothing to do with the behaviour you are describing/requesting.
In this case, since the user_id is only available after a successful login (and thus cannot be set in the loginRedirect
), you'll need to adjust your login action in your UsersController instead, to become something along the lines of:
if ($this->Auth->login()) {
return $this->redirect(array('controller' => 'users', 'action' => 'view', AuthComponent::user('id'));
}
Upvotes: 1