relative urls
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 ?
You should be able to get whatever you need using the URI class: http://codeigniter.com/user_guide/libraries/uri.html
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/
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']
Or use the anchor() helper:
echo anchor('controller/function', 'link');
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 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()
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
parse_url(base_url(), PHP_URL_PATH);