douyabu1528 2014-03-28 00:35
浏览 34
已采纳

通过CakePHP中的唯一字段路由数据

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));
    }
}
  • 写回答

1条回答 默认 最新

  • dousi6701 2014-03-29 15:07
    关注

    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));
            }
        }
     }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器