gpt4 book ai didi

php - 如何在 url 中附加用户名而不是 cakephp 3 中的 user_id

转载 作者:太空宇宙 更新时间:2023-11-03 22:31:19 27 4
gpt4 key购买 nike

我想要我的 url 这样的格式 http://localhost/blog/users/username而不是这个 http://localhost/blog/users/view/6

我在用户 View index.ctp 中有这段代码

enter image description here

<?php foreach ($users as $user): ?>
<?= $this->Html->link(__('View Profile'), ['action' => 'view', $user['user']['slug']]) ?>
<?php endforeach; ?>

路由.php

<?php
$routes->connect('/user/*', array('controller' => 'users', 'action' => 'view'));
?>


//public function view($id = null)
public function view($username)
{

$users = $this->Users->get($username, [
'contain' => ['Subjects'] // i have relation
]);
$this->set('users', $users);
$this->set('_serialize', ['user']);
}

我试过这个 link但它并没有解决我的问题

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']);
}

最佳答案

In index.ctp

<?php foreach ($users as $user): ?>
<?= $this->Html->link(__('View Profile'), ['action' => 'view', $user->username]) ?>
<?php endforeach; ?>

请根据您的结构更改$user->username

您无需在您的 routs.php

中执行任何操作

用户名将作为函数 View 的参数接收

function view($username){
//Your code
}

关于php - 如何在 url 中附加用户名而不是 cakephp 3 中的 user_id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48375100/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com