makuumba
makuumba

Reputation: 281

CodeIgniter -> Get current URL relative to base url

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?

Upvotes: 27

Views: 172633

Answers (11)

Sadam Hussain
Sadam Hussain

Reputation: 11

<?php $currentMenu =  $this->uri->segment(2); ?>
<ul>
   <li class="nav-item <?= ($currentMenu == 'dashboard') ?  'active' : '' ?>"> 
       <a href="<?= site_url('/admin/dashboard'); ?>" class="nav-link"><i data- 
         feather="pie-chart"></i> Dashboard</a>

   </li>
</ul

this is work for me

Upvotes: 0

AMIT KUMAR
AMIT KUMAR

Reputation: 1

you can use the some Codeigniter functions and some core functions and make combination to achieve your URL with query string. I found solution of this problem.

base_url($this->uri->uri_string()).strrchr($_SERVER['REQUEST_URI'], "?");

and if you loaded URL helper so you can also do this current_url().strrchr($_SERVER['REQUEST_URI'], "?");

Upvotes: 0

Barungi Stephen
Barungi Stephen

Reputation: 847

Running Latest Code Igniter 3.10

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

print base_url($this->uri->uri_string());

Upvotes: 3

Michael Krebs
Michael Krebs

Reputation: 1314

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"

Upvotes: 0

Danish Ali
Danish Ali

Reputation: 2352

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());

Upvotes: 13

Karim Mtl
Karim Mtl

Reputation: 1251

I see that this post is old. But in version CI v3 here is the answer:

echo $this->uri->uri_string();

Thanks

Upvotes: 10

user3024431
user3024431

Reputation: 461

If url helper is loaded, use

current_url();

will be better

Upvotes: 38

Nessler
Nessler

Reputation: 689

// For current url
echo base_url(uri_string());

Upvotes: 68

ashraf mohammed
ashraf mohammed

Reputation: 1350

 //if you want to get parameter from url use:
 parse_str($_SERVER['QUERY_STRING'], $_GET);
 //then you can use:
 if(isset($_GET["par"])){
      echo $_GET["par"];
 }
 //if you want to get current page url use:
 $current_url = current_url();

Upvotes: 5

GautamD31
GautamD31

Reputation: 28753

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

Upvotes: 13

Shimu
Shimu

Reputation: 1147

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.

Upvotes: 0

Related Questions