Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在url中附加用户名,而不是在cakephp 3中附加用户id_Php_Css_Cakephp_Cakephp 3.0 - Fatal编程技术网

如何在url中附加用户名,而不是在cakephp 3中附加用户id

如何在url中附加用户名,而不是在cakephp 3中附加用户id,php,css,cakephp,cakephp-3.0,Php,Css,Cakephp,Cakephp 3.0,我希望我的url采用这种格式,而不是这种格式 我在Users view index.ctp中有此代码 } 在index.ctp中 在index.ctp中 get函数使用模型的主键字段。您可以将主键更改为用户名,但我怀疑这会给您带来其他问题。相反,请尝试以下方法: $users = $this->Users->find('first') ->where(['username' => $username]) ->contain(['Subjects']

我希望我的url采用这种格式,而不是这种格式

我在Users view index.ctp中有此代码

}

在index.ctp中

在index.ctp中


get
函数使用模型的主键字段。您可以将主键更改为
用户名
,但我怀疑这会给您带来其他问题。相反,请尝试以下方法:

$users = $this->Users->find('first')
    ->where(['username' => $username])
    ->contain(['Subjects']);

另外,这里的变量是复数(
$users
)有什么原因吗?您应该只能从中获取一个用户,对吗?

函数使用模型的主键字段。您可以将主键更改为
用户名
,但我怀疑这会给您带来其他问题。相反,请尝试以下方法:

$users = $this->Users->find('first')
    ->where(['username' => $username])
    ->contain(['Subjects']);

另外,这里的变量是复数(
$users
)有什么原因吗?您应该只能从该站点获得一个用户,对吗?

@K.Dᴀᴠɪs抱歉,我已经包括routes.phpcode@K.Dᴀᴠɪs抱歉,我已经包含了routes.php代码。我应该向我的代码视图中添加什么方法来实现?目前,它显示了这个错误“在表“users”中找不到记录,主键为[NULL]”……在我尝试在上面添加view方法之后,我应该向view方法中的代码添加什么来实现?目前,它显示了这个错误“在表“users”中找不到记录,主键为[NULL]”……在我尝试添加上面的view方法后,它首先说“Unknown finder method”是的,当我单击view profile时,它只得到一个用户。我在上面的问题中添加了屏幕截图。像这样做
$user=$this->users->find()->where(['username'=>$username])->contain(['Subjects'])->first();
@AmanRawat哇,非常感谢,这样它就不会破坏cakephp或引起问题,就像Greg说的,我将为此创建另一个方法,并保留前面的方法“public function view,参数为$id”“@Greg,非常感谢您的帮助,,,AmanRawat,如果他们与主题没有关系,如何更改您的代码必须删除关联
$user=$this->Users->find()->where(['username'=>$username])->first();
首先显示“未知查找方法”“是的,当我单击视图配置文件时,它只得到一个用户。我在上面的问题中添加了屏幕截图。像这样做
$user=$this->Users->find()->其中(['username'=>$username])->包含(['Subjects')->第一个();
@AmanRawat哇,非常感谢,这样就不会破坏cakephp或引起问题,就像Greg说的那样,我将为此创建另一个方法,并保留以前的方法“参数为$id的公共函数视图”@Greg,非常感谢您的帮助,,,AmanRawat,如果他们与主题没有关系,如何更改代码必须删除关联
$user=$this->Users->find()->where(['username'=>$username])->first();
public function edit($id = null)
{
  //$logged_user_id=$this->request->Session()->read('Auth.user.id');


  $logged_user_id=$this->Auth->user('id');
  if($logged_user_id==$id){
      $user = $this->Users->get($id, [
        'contain' => []
    ]);
      if ($this->request->is(['patch', 'post', 'put'])) {
        $user = $this->Users->patchEntity($user, $this->request->getData());


        if ($this->Users->save($user)) {
            $this->Flash->success(__('User profile successfuly  updated.'));
            return $this->redirect(['action' => 'index']);
        } else {
            $this->Flash->error(__('The user could not be saved. Please, try again.'));
        }

    }
    $this->set(compact('user'));
    $this->set('_serialize', ['user']);
} else {
    $this->Flash->error(__('You are not allowed to do this.'));
    return $this->redirect(['action' => 'index']);
}
<?php foreach ($users as $user): ?>
<?= $this->Html->link(__('View Profile'), ['action' => 'view', $user->username]) ?>
<?php endforeach; ?>
function view($username){
    //Your code
}
$users = $this->Users->find('first')
    ->where(['username' => $username])
    ->contain(['Subjects']);