CodeIgniter -> Get current URL relative to base ur

2020-02-26 04:43发布

Tried URI::uri_string() but can't get it to work with the base_url.

URL: http://localhost/dropbox/derrek/shopredux/ahahaha/hihihi
Returns: dropbox/derrek/shopredux/ahahaha/hihihi

but http://localhost/dropbox/derrek/shopredux/ just returns an empty string.

I want the first call to return "ahahaha/hihihi" and the second to return "". Is there such a function?

9条回答
beautiful°
2楼-- · 2020-02-26 05:17

I don't know if there is such a function, but with $this->uri->uri_to_assoc() you get an associative array from the $_GET parameters. With this, and the controller you are in, you know how the URL looks like. In you above URL this would mean you would be in the controller dropbox and the array would be something like this:

array("derrek" => "shopredux", "ahahaha" => "hihihi");

With this you should be able to make such a function on your own.

查看更多
Lonely孤独者°
3楼-- · 2020-02-26 05:18

In CI v3, you can try:

function partial_uri($start = 0) {
    return join('/',array_slice(get_instance()->uri->segment_array(), $start));
}

This will drop the number of URL segments specified by the $start argument. If your URL is http://localhost/dropbox/derrek/shopredux/ahahaha/hihihi, then:

partial_uri(3);  # returns "ahahaha/hihihi"
查看更多
祖国的老花朵
4楼-- · 2020-02-26 05:20

Try to use "uri" segments like:

$this->uri->segment(5);   //To get 'ahahaha'
$this->uri->segment(6);   //To get 'hihihi

form your first URL...You get '' from second URl also for segment(5),segment(6) also because they are empty.

Every segment function counts starts form localhost as '1' and symultaneous segments

查看更多
Lonely孤独者°
5楼-- · 2020-02-26 05:21

Running Latest Code Igniter 3.10

$this->load->helper('uri'); // or you can autoload it in config

print base_url($this->uri->uri_string());
查看更多
仙女界的扛把子
6楼-- · 2020-02-26 05:23

For the parameter or without parameter URLs Use this :

Method 1:

 $currentURL = current_url(); //for simple URL
 $params = $_SERVER['QUERY_STRING']; //for parameters
 $fullURL = $currentURL . '?' . $params; //full URL with parameter

Method 2:

$full_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

Method 3:

base_url(uri_string());
查看更多
女痞
7楼-- · 2020-02-26 05:28
// For current url
echo base_url(uri_string());
查看更多
登录 后发表回答