How do you set up routing to view data via a field other than the ID in CakePHP? I'm trying to view my users via /username/'username'
in CakePHP 2.4.
So far, I have this in my routes.php, however I can't figure out how to pass the username to my Users controller and use that instead of an ID if needs be.
Router::connect(
'/username/:username',
array('controller' => 'users', 'action' => 'view'),
array(
'pass' => array('username')
)
);
And my controller function, I have this, which throws a Fatal error: Call to undefined function findByUsername():
public function view($id = null, $username = null) {
if ($user = $this->User-findByUsername($username)) {
$this->set('user', $user);
} elseif ($user != $this->User-findByUsername($username)) {
throw new NotFoundException(__('Invalid user'));
} else {
if (!$this->User->exists($id)) {
throw new NotFoundException(__('Invalid user'));
}
$options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
$this->set('user', $this->User->find('first', $options));
}
}