通过CakePHP中的唯一字段路由数据
如何设置路由以通过CakePHP中ID以外的字段查看数据?我试图通过CakePHP2.4中的通过CakePHP中的唯一字段路由数据,php,cakephp,routing,cakephp-2.0,Php,Cakephp,Routing,Cakephp 2.0,如何设置路由以通过CakePHP中ID以外的字段查看数据?我试图通过CakePHP2.4中的/username/'username'查看我的用户 到目前为止,我在我的routes.php中已经有了它,但是我不知道如何将用户名传递给我的用户控制器,并在需要时使用用户名而不是ID Router::connect( '/username/:username', array('controller' => 'users', 'action' => 'view'), a
/username/'username'
查看我的用户
到目前为止,我在我的routes.php中已经有了它,但是我不知道如何将用户名传递给我的用户控制器,并在需要时使用用户名而不是ID
Router::connect(
'/username/:username',
array('controller' => 'users', 'action' => 'view'),
array(
'pass' => array('username')
)
);
还有我的控制器函数,它抛出了一个致命错误:调用未定义的函数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));
}
}
第一种解决方案
不要对路由器做任何事情。它们与默认CakePHP包中的一样好。代码中有几个语法错误。当调用这两个函数时,它都应该工作
"app/controller/view/-/username"
或
控制器动作代码本身如下
<?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));
}
}
}
?>
或
和控制器动作代码本身:
<?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));
}
}
}
?>
…User-findByUsername()
->…User->findByUsername()
。否则,由于findBy…
是映射到表列的神奇函数,您的用户名字段是否区分大小写UserName
会有所不同……如果您不打算使用它,我还建议删除$id
参数,或者同时使用$UserName
参数,这取决于它是整数还是字符串……看起来应该可以工作,但是转到/users/view/username/
抛出错误:在此服务器上找不到请求的地址“/users/view/admin”。
,而/users/view/id/
抛出错误:SQLSTATE[42S22]:未找到列:1054 where子句中的未知列“User.id.id”
@schnaus:更新了我的答案,对不起,弄错了,行得通!我将看看如何修改它,使它只需要一个ID或用户名,而不需要URL中的-。我的一个改变就是删除了$id==''-')中的(
@schnauss:youcant。这是蛋糕内置的。如果控制器操作有两个参数,则在不同时传递第一个参数的情况下,无法设置第二个参数。这就是为什么url中需要有“-”或其他表示第一个参数值的内容。如果您想处理一个参数,请参阅我的更新答案。
"app/controller/view/username"
"app/controller/view/id"
<?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));
}
}
}
?>