【发布时间】:2012-02-13 21:53:36
【问题描述】:
我正在尝试为我的登录用户设置视图。我从 cakephp 2.0 手册中提取了代码。这是控制器代码:
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));
}
当 user_id 为 1 的用户登录时,cake 不会转到 /users/1。相反,它只是转到 /users/view。以下是与用户控制器有关的路由:
Router::connect('/users', array('controller' => 'users', 'action' => 'login'));
Router::connect('/users/:action', array('controller' => 'users'));
Router::connect('/users/:id', array('controller' => 'users', 'action' => 'view'));
我不确定我错了,是路由代码还是控制器代码。
更新新的控制器功能:
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));
}
【问题讨论】: