url codeigniter中的多个变量

问题描述 投票:1回答:1

我想将变量传递给方法。例如:

http://localhost/project/user/username/posts/postid

我想开始'posts'方法并有2个变量或一个assoc表

$user = username;
$posts = postid;

如果链接是http://localhost/project/user/username我想开始'索引'方法,并有变量$user = username

php codeigniter codeigniter-3
1个回答
1
投票

希望对你有帮助 :

像这样传递你的变量,URL结构应该是这样的:

http://localhost/base_folder/controller_name/method_name/username/post_id

假设您的控制器名称为welcome,方法名称为index,用户名为shamer,邮件ID为4;它会变成这样:

http://localhost/base_folder/welcome/index/shamer/4

在锚中应该是这样的:

<a href="<?=site_url('welcome/index/shamer/4');?>">your link</a>

Welcome控制器获取username并在你的id方法中发布index,如下所示:

public function index($username, $post_id)
{
   echo $username;
   echo $post_id;
}

更多信息:https://www.codeigniter.com/user_guide/general/controllers.html#passing-uri-segments-to-your-methods

© www.soinside.com 2019 - 2024. All rights reserved.