CodeIgniter icon indicating copy to clipboard operation
CodeIgniter copied to clipboard

relative urls

Open mirzadelic opened this issue 14 years ago • 8 comments

Hi, i use base_url() function to make full address, but i need relative url. THe reason for this is that i need this for davis.js or history.js plugin, so there i need relative urls, but i don't know how that works, becouse of /controller/function/param1/param2. Any solution, or some example ?

mirzadelic avatar Sep 04 '12 00:09 mirzadelic

You should be able to get whatever you need using the URI class: http://codeigniter.com/user_guide/libraries/uri.html

mwhitneysdsu avatar Sep 04 '12 04:09 mwhitneysdsu

But when i need to create link:

<a href="/controller/function">link</a>

and by base urls is not on level 1, url is like localhost/codeignter then this link will look: http://localhost/controller/function there is no /codeigniter/

mirzadelic avatar Sep 04 '12 09:09 mirzadelic

Please try following code:

<a href="<?php echo site_url('controller/functio'); ?>">link</a>

and make sure application/config/config.php config value

$config['base_url'] and $config['index_page']

appleboy avatar Sep 04 '12 09:09 appleboy

Or use the anchor() helper:

echo anchor('controller/function', 'link');

tkaw220 avatar Sep 04 '12 09:09 tkaw220

Both solutions create absolute url. I think best solution is to make new config variable and store /directory/ there and then in urls to call like this:

<a href="<?php echo $config['url_dir']; ?>controller/function">link</a>

and it will create link /codeigniter/controller/function without http://localhost/

Or new function like site_url, base_url that will do this thing.

mirzadelic avatar Sep 04 '12 10:09 mirzadelic

@mirzadelic if you are looking to reference directories, then you should also look at the Directory Helper.

You could use a combination of the directory helper and uri_string()

rizwhn avatar Sep 08 '12 10:09 rizwhn

From what I understand, you just need the base_url for your JS, right?

You can declare an inline JS global variable that sets your base URL. Do this before you call your other JS.

<script type="text/javascript">
var BASE_URL = "<?php echo base_url(); ?>";
</script>

Then, just append the variable to the URL paths that you need.

var my_url = BASE_URL + "foo/bar"; // Remember that base_url has a trailing slash

hubertursua avatar Sep 23 '12 15:09 hubertursua

parse_url(base_url(), PHP_URL_PATH);

narfbg avatar Oct 26 '12 20:10 narfbg