First solution
Don't do anything with routers. They are just fine as they are in default CakePHP package. There were couple of syntax errors in your code. This should work when called either
"app/controller/view/-/username"
or
"app/controller/view/id"
Controller action code itself like this
<?php
public function view($id, $username = null) {
if(!is_null($username)) {
if($user = $this->User->findByUsername($username))
$this->set('user', $user);
elseif (count($this->User->findByUsername($username)) == 0)
throw new NotFoundException(__('Invalid user'));
}
else {
if($id == '-') || !$this->User->exists($id))
throw new NotFoundException(__('Invalid user'));
else {
$options = array('conditions' => array("User.{$this->User->primaryKey}" => $id));
$this->set('user', $this->User->find('first', $options));
}
}
}
?>
Second solution
With only one parameter this could be done like this (personally I don't like this, it breaks straightforwardness of Cake-style to do it). Call this like this:
"app/controller/view/username"
or
"app/controller/view/id"
And controller action code itself:
<?php
public function view($param) {
if(!is_numeric($param))) { // $param is not numeric => it is username
if($user = $this->User->findByUsername($param))
$this->set('user', $user);
elseif (count($this->User->findByUsername($param)) == 0)
throw new NotFoundException(__('Invalid user'));
}
else { // $param is numeric => it is id
if(!$this->User->exists($param))
throw new NotFoundException(__('Invalid user'));
else {
$options = array('conditions' => array("User.{$this->User->primaryKey}" => $param));
$this->set('user', $this->User->find('first', $options));
}
}
}
?>