Codeigniter-如何在contoller函数中获取url变量值? [英] Codeigniter - How to get url variable value inside contoller function?

查看:92
本文介绍了Codeigniter-如何在contoller函数中获取url变量值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的URL:http://localhost/sitename/some-post-title/code=24639204963309423

现在我的控制器文件中有一个findUser函数

Now I have one findUser function in my controller file

public function findUser() {
   // I have tried with $_GET['code']
}

,并且我试图在此函数中获取code变量值.我已经尝试过$_GET['code'],但是没有用.

and I am trying to get code variable value inside this function. I have tried with $_GET['code'] but did not worked.

有什么想法可以在控制器功能中获得价值吗?

Any Idea how to get value inside controller function?

谢谢.

推荐答案

您要获取路径段变量还是GET变量?看来您要兼而有之.

Are you trying to get a path segment variable or a GET variable? It looks like you're going for a bit of both.

在CI中,如果您将URL更新为更像

Natively in CI, you can use $this->input->get if you update your url to look more like

http://localhost/sitename/some-post-title/?code=24639204963309423

(注意问号).

或者,您可以将URL修改为这样

Alternatively, you can modify your URL to look like this

http://localhost/sitename/some-post-title/code/24639204963309423

然后像这样使用URI段

And then use URI segments like so

$data = $this->uri->uri_to_assoc();
$code = $data['code'];

如果您不想更改URL,则必须像这样手动拆分该字符串

If you do not want to change your URL, you will have to break that string up manually like so

$data = $this->uri->segment(3);
$data = explode($data, '=');
$code = $data[1];

我认为第二个选择是最SEO友好且最漂亮的解决方案.但是这些功能在功能上都应该相同.

I would argue the second option is the most SEO-friendly and pretty solution. But each of these should be functionally identical.

这篇关于Codeigniter-如何在contoller函数中获取url变量值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆