我想将变量传递给方法。例如:
http://localhost/project/user/username/posts/postid
我想开始'posts'方法并有2个变量或一个assoc表
$user = username;
$posts = postid;
如果链接是http://localhost/project/user/username
我想开始'索引'方法,并有变量$user = username
希望对你有帮助 :
像这样传递你的变量,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