简体   繁体   English

在codeigniter中如何通过名称获取uri段?

[英]in codeigniter how can i get uri segments by name?

I have the latest codeigniter version, and was wondering how can i get the segments in a url by their parameter name. 我有最新的codeigniter版本,并想知道如何通过参数名称获取URL中的段。 For instance, here is a sample url: 例如,这是一个示例网址:

www.somewebsitedomain.com/param1=something/param2=somethingelse/

now lets say i want to get the value for 'param1', which is 'something', how would i do so using the uri class? 现在让我说我想得到'param1'的值,这是'某事',我怎么会这样使用uri类?

Because by default the uri class only gets segments by number, or the order in which they appear, but i want to get segments by the parameter name in that segment. 因为默认情况下,uri类只按编号或它们出现的顺序获取段,但我想通过该段中的参数名称获取段。 Or in general just get the parameter value. 或者通常只获取参数值。 Hope that makes sense... 希望有道理......

You could do $this->uri->uri_to_assoc(n) which will give something like the following 您可以执行$this->uri->uri_to_assoc(n) ,它将提供类似以下内容

[array]
 (
'name' => 'joe'
'location'  => 'UK'
'gender'    => 'male'
)

Then just just the param name you would like. 然后只是您想要的参数名称。

Source: http://codeigniter.com/user_guide/libraries/uri.html 资料来源: http//codeigniter.com/user_guide/libraries/uri.html

You could actually put them as GET vars and use the Input Class : 您实际上可以将它们作为GET变量并使用输入类

$param = $this->input->get('param1'); // something

or you can do: 或者你可以这样做:

$params = $this->input->get(); // array('param1' => 'something', 'param2' => 'somethingelse')

to get all the parameters 获得所有参数

I'm not sure this is what you're asking, but in many applications, URLs follow the form www.domain.com/controller/method/key1/value1/key2/value2. 我不确定这是你问的问题,但在许多应用程序中,URL的格式为www.domain.com/controller/method/key1/value1/key2/value2。 So I decided to extend the CI_URI class to return the value of any "key". 所以我决定扩展CI_URI类以返回任何“键”的值。

If you put this in your application/core folder, in a file named, MY_URI.php, it extends the built-in URI class: 如果将它放在application / core文件夹中,在名为MY_URI.php的文件中,它会扩展内置的URI类:

class MY_URI extends CI_URI {

    /* call parent constructor */
    function __construct() {
        parent::__construct();
    }

    /* return value of the URI segment
       which immediately follows the named segment */
    function getNamed($str=NULL) {

        $key = array_search($str, $this->segments);

        if ($key && isset($this->segments[$key+1])) {
            return $this->segments[$key+1];
        }

        return false;

    }

}

Then if your URLs are in the form 然后,如果您的网址在表单中

www.somewebsitedomain.com/param1/something/param2/somethingelse/

you can call this function as follows: 您可以按如下方式调用此函数:

$this->uri->getNamed('param1) - returns "something" $this->uri->getNamed('param1) - 返回“某事”

$this->uri->getNamed('param2) - returns "somethingelse" $this->uri->getNamed('param2) - 返回“somethingelse”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM